<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>大数据之数据仓库Hive03_查询和 Hive 函数 | Oct25-X</title><meta name="keywords" content="Hive"><meta name="author" content="Oct25-X"><meta name="copyright" content="Oct25-X"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="查询一、查询基础语法 官方网址  https:&#x2F;&#x2F;cwiki.apache.org&#x2F;confluence&#x2F;display&#x2F;Hive&#x2F;LanguageManual+Select  查询语法  12345678SELECT [ALL | DISTINCT] select_expr, select_expr, ...FROM table_reference       -- 从什么表查[WHERE wh">
<meta property="og:type" content="article">
<meta property="og:title" content="大数据之数据仓库Hive03_查询和 Hive 函数">
<meta property="og:url" content="https://oct25-x.atomgit.net/oct25-xxxxx/2023/07/18/15%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive03_%E6%9F%A5%E8%AF%A2%E5%92%8C%20Hive%20%E5%87%BD%E6%95%B0/index.html">
<meta property="og:site_name" content="Oct25-X">
<meta property="og:description" content="查询一、查询基础语法 官方网址  https:&#x2F;&#x2F;cwiki.apache.org&#x2F;confluence&#x2F;display&#x2F;Hive&#x2F;LanguageManual+Select  查询语法  12345678SELECT [ALL | DISTINCT] select_expr, select_expr, ...FROM table_reference       -- 从什么表查[WHERE wh">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt15.png">
<meta property="article:published_time" content="2023-07-18T11:33:29.000Z">
<meta property="article:modified_time" content="2024-02-17T14:17:17.222Z">
<meta property="article:author" content="Oct25-X">
<meta property="article:tag" content="Hive">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt15.png"><link rel="shortcut icon" href="/oct25-xxxxx/img/logo.jpg"><link rel="canonical" href="https://oct25-x.atomgit.net/oct25-xxxxx/2023/07/18/15%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive03_%E6%9F%A5%E8%AF%A2%E5%92%8C%20Hive%20%E5%87%BD%E6%95%B0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/oct25-xxxxx/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/oct25-xxxxx/',
  algolia: undefined,
  localSearch: {"path":"search.json","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '大数据之数据仓库Hive03_查询和 Hive 函数',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-17 22:17:17'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/rightMenu.css"><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg14.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/oct25-xxxxx/">Oct25-X</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">大数据之数据仓库Hive03_查询和 Hive 函数</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-07-18T11:33:29.000Z" title="发表于 2023-07-18 19:33:29">2023-07-18</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-17T14:17:17.222Z" title="更新于 2024-02-17 22:17:17">2024-02-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/oct25-xxxxx/categories/%E5%A4%A7%E6%95%B0%E6%8D%AE%E7%BB%84%E4%BB%B6/">大数据组件</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="大数据之数据仓库Hive03_查询和 Hive 函数"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h1><h2 id="一、查询基础语法"><a href="#一、查询基础语法" class="headerlink" title="一、查询基础语法"></a>一、查询基础语法</h2><ol>
<li><strong>官方网址</strong></li>
</ol>
<p><a target="_blank" rel="noopener" href="https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select">https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select</a></p>
<ol start="2">
<li><strong>查询语法</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> [<span class="keyword">ALL</span> <span class="operator">|</span> <span class="keyword">DISTINCT</span>] select_expr, select_expr, ...</span><br><span class="line"><span class="keyword">FROM</span> table_reference       <span class="comment">-- 从什么表查</span></span><br><span class="line">[<span class="keyword">WHERE</span> where_condition]   <span class="comment">-- 过滤</span></span><br><span class="line">[<span class="keyword">GROUP</span> <span class="keyword">BY</span> col_list]        <span class="comment">-- 分组查询</span></span><br><span class="line">[<span class="keyword">HAVING</span> col_list]          <span class="comment">-- 分组后过滤</span></span><br><span class="line">[<span class="keyword">ORDER</span> <span class="keyword">BY</span> col_list]        <span class="comment">-- 排序</span></span><br><span class="line">[CLUSTER <span class="keyword">BY</span> col_list <span class="operator">|</span> [DISTRIBUTE <span class="keyword">BY</span> col_list] [SORT <span class="keyword">BY</span> col_list]]</span><br><span class="line">[LIMIT number]                <span class="comment">-- 限制输出的行数</span></span><br></pre></td></tr></table></figure>

<h2 id="二、基础查询"><a href="#二、基础查询" class="headerlink" title="二、基础查询"></a>二、基础查询</h2><h3 id="数据准备"><a href="#数据准备" class="headerlink" title="数据准备"></a>数据准备</h3><p>在 &#x2F;opt&#x2F;module&#x2F;hive-3.1.3&#x2F;datas 目录下，创建数据文件</p>
<p><strong>dept.txt</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">10	行政部	1700</span><br><span class="line">20	财务部	1800</span><br><span class="line">30	教学部	1900</span><br><span class="line">40	销售部	1700</span><br></pre></td></tr></table></figure>

<p>emp.txt</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">7369	张三	研发	800.00	30</span><br><span class="line">7499	李四	财务	1600.00	20</span><br><span class="line">7521	王五	行政	1250.00	10</span><br><span class="line">7566	赵六	销售	2975.00	40</span><br><span class="line">7654	侯七	研发	1250.00	30</span><br><span class="line">7698	马八	研发	2850.00	30</span><br><span class="line">7782	金九	\N	2450.0	30</span><br><span class="line">7788	银十	行政	3000.00	10</span><br><span class="line">7839	小芳	销售	5000.00	40</span><br><span class="line">7844	小明	销售	1500.00	40</span><br><span class="line">7876	小李	行政	1100.00	10</span><br><span class="line">7900	小元	讲师	950.00	30</span><br><span class="line">7902	小海	行政	3000.00	10</span><br><span class="line">7934	小红明	讲师	1300.00	30</span><br></pre></td></tr></table></figure>

<p>location.txt</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1700	北京</span><br><span class="line">1800	上海</span><br><span class="line">1900	深圳</span><br></pre></td></tr></table></figure>

<p>在 Hive 上，创建对应的表</p>
<p><strong>dept 表</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> dept (deptno <span class="type">int</span>, dname string, loc <span class="type">int</span>)</span><br><span class="line"><span class="type">row</span> format delimited fields terminated <span class="keyword">by</span> <span class="string">&#x27;\t&#x27;</span>;</span><br></pre></td></tr></table></figure>

<p><strong>emp 表</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> emp (empno <span class="type">int</span>, empname string, job string, sal <span class="keyword">double</span>, deptno <span class="type">int</span>)</span><br><span class="line"><span class="type">row</span> format delimited fields terminated <span class="keyword">by</span> <span class="string">&#x27;\t&#x27;</span>;</span><br></pre></td></tr></table></figure>

<p><strong>location 表</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> location (loc <span class="type">int</span>, loc_name string)</span><br><span class="line"><span class="type">row</span> format delimited fields terminated <span class="keyword">by</span> <span class="string">&#x27;\t&#x27;</span>;</span><br></pre></td></tr></table></figure>



<p>将数据文件中的数据，导入到 Hive 中</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">load data <span class="keyword">local</span> inpath <span class="string">&#x27;/opt/module/hive-3.1.3/datas/dept.txt&#x27;</span> <span class="keyword">into</span> <span class="keyword">table</span> dept;</span><br><span class="line">load data <span class="keyword">local</span> inpath <span class="string">&#x27;/opt/module/hive-3.1.3/datas/emp.txt&#x27;</span> <span class="keyword">into</span> <span class="keyword">table</span> emp;</span><br><span class="line">load data <span class="keyword">local</span> inpath <span class="string">&#x27;/opt/module/hive-3.1.3/datas/location.txt&#x27;</span> <span class="keyword">into</span> <span class="keyword">table</span> location;</span><br></pre></td></tr></table></figure>

<h3 id="全表和特定列查询"><a href="#全表和特定列查询" class="headerlink" title="全表和特定列查询"></a>全表和特定列查询</h3><ol>
<li><strong>全表查询</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> dept;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>特定列查询</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> deptno, dname <span class="keyword">from</span> dept;</span><br></pre></td></tr></table></figure>

<p>注意：大小写不敏感</p>
<h3 id="列别名（AS）"><a href="#列别名（AS）" class="headerlink" title="列别名（AS）"></a>列别名（AS）</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> empname <span class="keyword">as</span> en, deptno <span class="keyword">as</span> dno <span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>

<h3 id="Limit-语句"><a href="#Limit-语句" class="headerlink" title="Limit 语句"></a>Limit 语句</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp limit <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--从第2行开始（下标0开始），向下抓取3行</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp limit <span class="number">2</span>, <span class="number">3</span>;</span><br></pre></td></tr></table></figure>

<h3 id="Where-语句"><a href="#Where-语句" class="headerlink" title="Where 语句"></a>Where 语句</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> sal <span class="operator">&gt;</span> <span class="number">1000</span>;</span><br></pre></td></tr></table></figure>

<p>注意：where 子句不能使用字段别名</p>
<h3 id="关系运算函数"><a href="#关系运算函数" class="headerlink" title="关系运算函数"></a>关系运算函数</h3><p>如下操作符主要应用于 where 和 having 语句中</p>
<table>
<thead>
<tr>
<th>操作符</th>
<th>支持的数据类型</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>A&#x3D;B</td>
<td>基本数据类型</td>
<td>如果A等于B则返回true，反之返回false</td>
</tr>
<tr>
<td>A&lt;&#x3D;&gt;B</td>
<td>基本数据类型</td>
<td>如果A和B都为null或者都不为null，则返回true，如果只有一边为null，返回false</td>
</tr>
<tr>
<td>A&lt;&gt;B, A!&#x3D;B</td>
<td>基本数据类型</td>
<td>A或者B为null则返回null；如果A不等于B，则返回true，反之返回false</td>
</tr>
<tr>
<td>A&lt;B</td>
<td>基本数据类型</td>
<td>A或者B为null，则返回null；如果A小于B，则返回true，反之返回false</td>
</tr>
<tr>
<td>A&lt;&#x3D;B</td>
<td>基本数据类型</td>
<td>A或者B为null，则返回null；如果A小于等于B，则返回true，反之返回false</td>
</tr>
<tr>
<td>A&gt;B</td>
<td>基本数据类型</td>
<td>A或者B为null，则返回null；如果A大于B，则返回true，反之返回false</td>
</tr>
<tr>
<td>A&gt;&#x3D;B</td>
<td>基本数据类型</td>
<td>A或者B为null，则返回null；如果A大于等于B，则返回true，反之返回false</td>
</tr>
<tr>
<td>A [not] between B and C</td>
<td>基本数据类型</td>
<td>如果A，B或者C任一为null，则结果为null。<br/>如果A的值大于等于B而且小于或等于C，则结果为true，反之为false。如果使用not关键字则可达到相反的效果。</td>
</tr>
<tr>
<td>A is null</td>
<td>所有数据类型</td>
<td>如果A等于null，则返回true，反之返回false</td>
</tr>
<tr>
<td>A is not null</td>
<td>所有数据类型</td>
<td>如果A不等于null，则返回true，反之返回false</td>
</tr>
<tr>
<td>in（数值1，数值2）</td>
<td>所有数据类型</td>
<td>使用 in运算显示列表中的值</td>
</tr>
<tr>
<td>A [not] like B</td>
<td>string类型</td>
<td>B是一个SQL下的简单正则表达式，也叫通配符模式，如果A与其匹配的话，则返回true；反之返回false。<br/>B的表达式说明如下：<br/>‘x%’表示A必须以字母‘x’开头，‘%x’表示A必须以字母‘x’结尾，而‘%x%’表示A包含有字母‘x’,可以位于开头，结尾或者字符串中间。<br/>如果使用not关键字则可达到相反的效果。</td>
</tr>
<tr>
<td>A rlike B, A regexp B</td>
<td>string类型</td>
<td>B是基于java的正则表达式，如果A与其匹配，则返回true；反之返回false。<br/>匹配使用的是JDK中的正则表达式接口实现的，因为正则也依据其中的规则。<br/>例如，正则表达式必须和整个字符串A相匹配，而不是只需与其字符串匹配。</td>
</tr>
</tbody></table>
<h3 id="逻辑运算函数"><a href="#逻辑运算函数" class="headerlink" title="逻辑运算函数"></a>逻辑运算函数</h3><table>
<thead>
<tr>
<th>操作符</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>and</td>
<td>逻辑并</td>
</tr>
<tr>
<td>or</td>
<td>逻辑或</td>
</tr>
<tr>
<td>not</td>
<td>逻辑否</td>
</tr>
</tbody></table>
<ol>
<li>查询薪水大于1000，部门编号是30的员工信息</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> sal <span class="operator">&gt;</span> <span class="number">1000</span> <span class="keyword">and</span> deptno <span class="operator">=</span> <span class="number">30</span>;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li>查询薪水大于1000，或者部门编号是30的员工信息</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> sal <span class="operator">&gt;</span> <span class="number">1000</span> <span class="keyword">or</span> deptno <span class="operator">=</span> <span class="number">30</span>;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>查询除了部门编号为20和30以外的员工信息</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> deptno <span class="keyword">not</span> <span class="keyword">in</span> (<span class="number">20</span>, <span class="number">30</span>);</span><br></pre></td></tr></table></figure>

<h3 id="聚合函数"><a href="#聚合函数" class="headerlink" title="聚合函数"></a>聚合函数</h3><p>*<em>count(</em>)**：表示统计所有行数，包含 null 值</p>
<p>**count(某列)**：表示该列一共有多少行，不包含 null 值</p>
<p>**max()**：最大值，不包含 null，除非所有值都是 null</p>
<p>**min()**：最小值，不包含 null，除非所有值都是 null</p>
<p>**sum()**：最和，不包含 null</p>
<p>**avg()**：平均值，不包含 null</p>
<ol>
<li><strong>求总行数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>) cnt <span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>求最高薪水</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">max</span>(sal) cnt <span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>求最少薪水</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">min</span>(sal) cnt <span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>求薪水总和</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">sum</span>(sal) cnt <span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>

<ol start="5">
<li><strong>求薪水平均值</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">avg</span>(sal) cnt <span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>

<h2 id="三、分组"><a href="#三、分组" class="headerlink" title="三、分组"></a>三、分组</h2><h3 id="Group-By-语句"><a href="#Group-By-语句" class="headerlink" title="Group By 语句"></a>Group By 语句</h3><p>Group By 语句通常会和聚合函数一起使用，按照一个或者多个列队结果进行分组，然后对每个组执行聚合操作。</p>
<ol>
<li><strong>计算员工每个部门平均薪水</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> e.deptno, <span class="built_in">avg</span>(sal) <span class="keyword">from</span> emp e <span class="keyword">group</span> <span class="keyword">by</span> e.deptno;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>计算员工每个部门中每个岗位最高薪水</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> e.deptno, e.job, <span class="built_in">max</span>(sal) <span class="keyword">from</span> emp e <span class="keyword">group</span> <span class="keyword">by</span> e.deptno, e.job;</span><br></pre></td></tr></table></figure>

<h3 id="Having-语句"><a href="#Having-语句" class="headerlink" title="Having 语句"></a>Having 语句</h3><p>having 和 where 不同点</p>
<ul>
<li>where 后面不可以使用分组聚合函数，而 having 可以使用</li>
<li>having 只用于 group by 分组统计后</li>
</ul>
<ol>
<li><strong>求每个部门的平均薪水大于2000的部门</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> e.deptno, <span class="built_in">avg</span>(e.sal) avg_sal <span class="keyword">from</span> emp e <span class="keyword">group</span> <span class="keyword">by</span> e.deptno <span class="keyword">having</span> <span class="built_in">avg</span>(e.sal) <span class="operator">&gt;</span> <span class="number">2000</span>;</span><br></pre></td></tr></table></figure>

<h2 id="四、Join-语句"><a href="#四、Join-语句" class="headerlink" title="四、Join 语句"></a>四、Join 语句</h2><p>Hive 只支持等值连接，不支持非等值连接。</p>
<h3 id="等值-Join"><a href="#等值-Join" class="headerlink" title="等值 Join"></a>等值 Join</h3><p>根据员工表和部门表中的部门编号，查询员工编号，员工姓名和部门名称</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> e.empno , e.empname, d.dname <span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">join</span> dept d <span class="keyword">on</span> e.deptno <span class="operator">=</span> d.deptno;</span><br></pre></td></tr></table></figure>

<h3 id="内连接"><a href="#内连接" class="headerlink" title="内连接"></a>内连接</h3><p>内连接：只有进行连接的两个表中都存在与连接条件相匹配的数据才会被保留下来</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> e.empno , e.empname, d.dname <span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">join</span> dept d <span class="keyword">on</span> e.deptno <span class="operator">=</span> d.deptno;</span><br></pre></td></tr></table></figure>

<h3 id="左-x2F-右外连接"><a href="#左-x2F-右外连接" class="headerlink" title="左&#x2F;右外连接"></a>左&#x2F;右外连接</h3><p>左外连接：join 操作符左边表中符合 where 子句的所有记录将会被返回</p>
<p>右外连接：join 操作符右边表中符合 where 子句的所有记录将会被返回</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--左外连接</span></span><br><span class="line"><span class="keyword">select</span> e.empno , e.empname, d.dname <span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">left</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.deptno <span class="operator">=</span> d.deptno;</span><br><span class="line"></span><br><span class="line"><span class="comment">--右外连接</span></span><br><span class="line"><span class="keyword">select</span> e.empno , e.empname, d.dname <span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">right</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.deptno <span class="operator">=</span> d.deptno;</span><br></pre></td></tr></table></figure>

<h3 id="满外连接"><a href="#满外连接" class="headerlink" title="满外连接"></a>满外连接</h3><p>满外连接：将会返回所有表中符合 where 语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话，那么就使用 null 值替代</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> e.empno , e.empname, d.dname <span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">full</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.deptno <span class="operator">=</span> d.deptno;</span><br></pre></td></tr></table></figure>

<h3 id="多表连接"><a href="#多表连接" class="headerlink" title="多表连接"></a>多表连接</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> e.empname, d.dname, l.loc_name</span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">join</span> dept d <span class="keyword">on</span> e.deptno <span class="operator">=</span> d.deptno</span><br><span class="line"><span class="keyword">join</span> location l <span class="keyword">on</span> d.loc <span class="operator">=</span> l.loc;</span><br></pre></td></tr></table></figure>

<p>大多数情况下，Hive 会对每对 join 连接对象启动一个 MapReduce 任务。</p>
<p>本例中会首先启动一个 MapReduce job 对表 e 和表 d 进行连接操作，然后会再启动一个 MapReduce job 将第一个MapReduce job 的输出和表l进行连接操作。</p>
<p>注意：为什么不是表d和表l先进行连接操作呢？这是因为 Hive 总是按照从左到右的顺序执行的</p>
<h3 id="笛卡尔集"><a href="#笛卡尔集" class="headerlink" title="笛卡尔集"></a>笛卡尔集</h3><p>禁用</p>
<h3 id="联合（union-amp-union-all）"><a href="#联合（union-amp-union-all）" class="headerlink" title="联合（union &amp; union all）"></a>联合（union &amp; union all）</h3><p>join 是左右关联，union 和 union all 是上下拼接</p>
<p><code>union 去重，union all 不去重</code></p>
<p>union 和 union all 在上下拼接 sql 结果时有两个要求：</p>
<ul>
<li><p>两个 sql 的结果，列的个数必须相同</p>
</li>
<li><p>两个 sql 的结果，上下所对应列的类型必须一致</p>
</li>
</ul>
<ol>
<li><strong>将员工表部门编号为30的员工信息和部门编号为40的员工信息，使用 union 进行拼接</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> deptno <span class="operator">=</span> <span class="number">30</span></span><br><span class="line"><span class="keyword">union</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> deptno <span class="operator">=</span> <span class="number">40</span>;</span><br></pre></td></tr></table></figure>

<h2 id="五、排序"><a href="#五、排序" class="headerlink" title="五、排序"></a>五、排序</h2><h3 id="全局排序（Order-By）"><a href="#全局排序（Order-By）" class="headerlink" title="全局排序（Order By）"></a>全局排序（Order By）</h3><p>order by：全局排序，只有一个 Reduce</p>
<p>使用 order by 子句排序，asc（ascend）升序（默认），desc（descend）降序</p>
<ol>
<li><strong>员工信息按照薪水降序排序</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> sal <span class="keyword">desc</span>;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>按照部门升序和薪水降序排序</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> deptno, sal <span class="keyword">desc</span>;</span><br></pre></td></tr></table></figure>

<h3 id="每个-Reduce-内部排序（Sort-By）"><a href="#每个-Reduce-内部排序（Sort-By）" class="headerlink" title="每个 Reduce 内部排序（Sort By）"></a>每个 Reduce 内部排序（Sort By）</h3><p>对于大规模的数据集 order by 的效率非常低。在很多情况下，并不需要全局排序，此时可以使用 sort<br>by</p>
<p>sort by 为每个 reduce 产生一个排序文件。每个 Reduce 内部进行排序，对全局结果集来说不是排序</p>
<ol>
<li><strong>设置 Reduce 个数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> mapreduce.job.reduces<span class="operator">=</span><span class="number">3</span>;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查看 Reduce 个数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> mapreduce.job.reduces;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>根据部门编号降序查看员工信息</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp sort <span class="keyword">by</span> deptno <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line"><span class="operator">+</span><span class="comment">------------+--------------+----------+----------+-------------+</span></span><br><span class="line"><span class="operator">|</span> emp.empno  <span class="operator">|</span> emp.empname  <span class="operator">|</span> emp.job  <span class="operator">|</span> emp.sal  <span class="operator">|</span> emp.deptno  <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">------------+--------------+----------+----------+-------------+</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7844</span>       <span class="operator">|</span> 小明           <span class="operator">|</span> 销售       <span class="operator">|</span> <span class="number">1500.0</span>   <span class="operator">|</span> <span class="number">40</span>       <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7839</span>       <span class="operator">|</span> 小芳           <span class="operator">|</span> 销售       <span class="operator">|</span> <span class="number">5000.0</span>   <span class="operator">|</span> <span class="number">40</span>       <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7782</span>       <span class="operator">|</span> 金九           <span class="operator">|</span> <span class="keyword">NULL</span>      <span class="operator">|</span> <span class="number">2450.0</span>   <span class="operator">|</span> <span class="number">30</span>        <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7698</span>       <span class="operator">|</span> 马八           <span class="operator">|</span> 研发       <span class="operator">|</span> <span class="number">2850.0</span>   <span class="operator">|</span> <span class="number">30</span>       <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7654</span>       <span class="operator">|</span> 侯七           <span class="operator">|</span> 研发       <span class="operator">|</span> <span class="number">1250.0</span>   <span class="operator">|</span> <span class="number">30</span>       <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7788</span>       <span class="operator">|</span> 银十           <span class="operator">|</span> 行政       <span class="operator">|</span> <span class="number">3000.0</span>   <span class="operator">|</span> <span class="number">10</span>       <span class="operator">|</span> <span class="comment">--- 文件1</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7566</span>       <span class="operator">|</span> 赵六           <span class="operator">|</span> 销售       <span class="operator">|</span> <span class="number">2975.0</span>   <span class="operator">|</span> <span class="number">40</span>       <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7934</span>       <span class="operator">|</span> 小红明          <span class="operator">|</span> 讲师       <span class="operator">|</span> <span class="number">1300.0</span>   <span class="operator">|</span> <span class="number">30</span>      <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7900</span>       <span class="operator">|</span> 小元           <span class="operator">|</span> 讲师       <span class="operator">|</span> <span class="number">950.0</span>    <span class="operator">|</span> <span class="number">30</span>       <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7499</span>       <span class="operator">|</span> 李四           <span class="operator">|</span> 财务       <span class="operator">|</span> <span class="number">1600.0</span>   <span class="operator">|</span> <span class="number">20</span>       <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7876</span>       <span class="operator">|</span> 小李           <span class="operator">|</span> 行政       <span class="operator">|</span> <span class="number">1100.0</span>   <span class="operator">|</span> <span class="number">10</span>       <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7521</span>       <span class="operator">|</span> 王五           <span class="operator">|</span> 行政       <span class="operator">|</span> <span class="number">1250.0</span>   <span class="operator">|</span> <span class="number">10</span>       <span class="operator">|</span> <span class="comment">-- 文件2</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7369</span>       <span class="operator">|</span> 张三           <span class="operator">|</span> 研发       <span class="operator">|</span> <span class="number">800.0</span>    <span class="operator">|</span> <span class="number">30</span>       <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> <span class="number">7902</span>       <span class="operator">|</span> 小海           <span class="operator">|</span> 行政       <span class="operator">|</span> <span class="number">3000.0</span>   <span class="operator">|</span> <span class="number">10</span>       <span class="operator">|</span> <span class="comment">-- 文件3</span></span><br><span class="line"><span class="operator">+</span><span class="comment">------------+--------------+----------+----------+-------------+</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>将查询结果导入文件中</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">local</span> directory <span class="string">&#x27;/opt/module/hive-3.1.3/datas/sortby-res&#x27;</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp sort <span class="keyword">by</span> deptno <span class="keyword">desc</span>;</span><br></pre></td></tr></table></figure>

<h3 id="分区（Distribute-By）"><a href="#分区（Distribute-By）" class="headerlink" title="分区（Distribute By）"></a>分区（Distribute By）</h3><p>在有些情况下，我们需要控制某个特定行应该到哪个 Reducer，通常是为了进行后续的聚集操作。distribute by 子句可以做这件事。</p>
<p>distribute by 类似 MapReduce 中 partition（自定义分区），进行分区，结合 sort by 使用。 </p>
<p>对于 distribute by 进行测试，一定要分配多 reduce 进行处理，否则无法看到 distribute by 的效果。</p>
<ol>
<li><strong>先按照部门编号分区，再按照员工薪水排序</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">local</span> directory <span class="string">&#x27;/opt/module/hive-3.1.3/datas/distribute-res&#x27;</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp distribute <span class="keyword">by</span> deptno sort <span class="keyword">by</span> sal <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--文件 000000_0</span></span><br><span class="line"><span class="number">7698</span>	马八	研发	<span class="number">2850.0</span>	<span class="number">30</span></span><br><span class="line"><span class="number">7782</span>	金九	\N	<span class="number">2450.0</span>	<span class="number">30</span></span><br><span class="line"><span class="number">7934</span>	小红明	讲师	<span class="number">1300.0</span>	<span class="number">30</span></span><br><span class="line"><span class="number">7654</span>	侯七	研发	<span class="number">1250.0</span>	<span class="number">30</span></span><br><span class="line"><span class="number">7900</span>	小元	讲师	<span class="number">950.0</span>	<span class="number">30</span></span><br><span class="line"><span class="number">7369</span>	张三	研发	<span class="number">800.0</span>	<span class="number">30</span></span><br><span class="line"></span><br><span class="line"><span class="comment">--文件 000001_0</span></span><br><span class="line"><span class="number">7839</span>	小芳	销售	<span class="number">5000.0</span>	<span class="number">40</span></span><br><span class="line"><span class="number">7788</span>	银十	行政	<span class="number">3000.0</span>	<span class="number">10</span></span><br><span class="line"><span class="number">7902</span>	小海	行政	<span class="number">3000.0</span>	<span class="number">10</span></span><br><span class="line"><span class="number">7566</span>	赵六	销售	<span class="number">2975.0</span>	<span class="number">40</span></span><br><span class="line"><span class="number">7844</span>	小明	销售	<span class="number">1500.0</span>	<span class="number">40</span></span><br><span class="line"><span class="number">7521</span>	王五	行政	<span class="number">1250.0</span>	<span class="number">10</span></span><br><span class="line"><span class="number">7876</span>	小李	行政	<span class="number">1100.0</span>	<span class="number">10</span></span><br><span class="line"></span><br><span class="line"><span class="comment">--文件 000002_0</span></span><br><span class="line"><span class="number">7499</span>	李四	财务	<span class="number">1600.0</span>	<span class="number">20</span></span><br></pre></td></tr></table></figure>

<p>distribute by 的分区规则是根据分区字段的 hash 码与 reduce 的个数进行模除后，余数相同的分到一个区。</p>
<p>Hive 要求 DISTRIBUTE BY 语句要写在 SORT BY 语句之前。</p>
<h3 id="分区排序（Cluster-By）"><a href="#分区排序（Cluster-By）" class="headerlink" title="分区排序（Cluster By）"></a>分区排序（Cluster By）</h3><p>当 distribute by 和 sorts by 字段相同时，可以使用 cluster by 方式。</p>
<p>cluster by 除了具有 distribute by 的功能外还兼具 sort by 的功能。但是排序只能是升序排序，不能指定排序规则为 ASC 或者 DESC。</p>
<p><strong>Cluster By</strong></p>
<p><strong>当 Sort By 与 Distribute By 两个字段相同时，可以使用 Cluster By 代替。</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 以下两种写法等价</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp cluster <span class="keyword">by</span> deptno;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp distribute <span class="keyword">by</span> deptno sort <span class="keyword">by</span> deptno;</span><br></pre></td></tr></table></figure>

<h1 id="综合案例练习一"><a href="#综合案例练习一" class="headerlink" title="综合案例练习一"></a>综合案例练习一</h1><h2 id="一、数据准备"><a href="#一、数据准备" class="headerlink" title="一、数据准备"></a>一、数据准备</h2><h3 id="建表"><a href="#建表" class="headerlink" title="建表"></a>建表</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 创建学生表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> student;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> student_info(</span><br><span class="line">    stu_id string COMMENT <span class="string">&#x27;学生id&#x27;</span>,</span><br><span class="line">    stu_name string COMMENT <span class="string">&#x27;学生姓名&#x27;</span>,</span><br><span class="line">    birthday string COMMENT <span class="string">&#x27;出生日期&#x27;</span>,</span><br><span class="line">    sex string COMMENT <span class="string">&#x27;性别&#x27;</span></span><br><span class="line">) </span><br><span class="line"><span class="type">row</span> format delimited fields terminated <span class="keyword">by</span> <span class="string">&#x27;,&#x27;</span> </span><br><span class="line">stored <span class="keyword">as</span> textfile;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建课程表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> course;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> course_info(</span><br><span class="line">    course_id string COMMENT <span class="string">&#x27;课程id&#x27;</span>,</span><br><span class="line">    course_name string COMMENT <span class="string">&#x27;课程名&#x27;</span>,</span><br><span class="line">    tea_id string COMMENT <span class="string">&#x27;任课老师id&#x27;</span></span><br><span class="line">) </span><br><span class="line"><span class="type">row</span> format delimited fields terminated <span class="keyword">by</span> <span class="string">&#x27;,&#x27;</span> </span><br><span class="line">stored <span class="keyword">as</span> textfile;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建老师表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> teacher;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> teacher_info(</span><br><span class="line">    tea_id string COMMENT <span class="string">&#x27;老师id&#x27;</span>,</span><br><span class="line">    tea_name string COMMENT <span class="string">&#x27;学生姓名&#x27;</span></span><br><span class="line">) </span><br><span class="line"><span class="type">row</span> format delimited fields terminated <span class="keyword">by</span> <span class="string">&#x27;,&#x27;</span> </span><br><span class="line">stored <span class="keyword">as</span> textfile;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建分数表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> score;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> score_info(</span><br><span class="line">    stu_id string COMMENT <span class="string">&#x27;学生id&#x27;</span>,</span><br><span class="line">    course_id string COMMENT <span class="string">&#x27;课程id&#x27;</span>,</span><br><span class="line">    score <span class="type">int</span> COMMENT <span class="string">&#x27;成绩&#x27;</span></span><br><span class="line">) </span><br><span class="line"><span class="type">row</span> format delimited fields terminated <span class="keyword">by</span> <span class="string">&#x27;,&#x27;</span> </span><br><span class="line">stored <span class="keyword">as</span> textfile;</span><br></pre></td></tr></table></figure>

<h3 id="数据文件"><a href="#数据文件" class="headerlink" title="数据文件"></a>数据文件</h3><p><strong>student_info.txt</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">001,彭于晏,1995-05-16,男</span><br><span class="line">002,胡歌,1994-03-20,男</span><br><span class="line">003,周杰伦,1995-04-30,男</span><br><span class="line">004,刘德华,1998-08-28,男</span><br><span class="line">005,唐国强,1993-09-10,男</span><br><span class="line">006,陈道明,1992-11-12,男</span><br><span class="line">007,陈坤,1999-04-09,男</span><br><span class="line">008,吴京,1994-02-06,男</span><br><span class="line">009,郭德纲,1992-12-05,男</span><br><span class="line">010,于谦,1998-08-23,男</span><br><span class="line">011,潘长江,1995-05-27,男</span><br><span class="line">012,杨紫,1996-12-21,女</span><br><span class="line">013,蒋欣,1997-11-08,女</span><br><span class="line">014,赵丽颖,1990-01-09,女</span><br><span class="line">015,刘亦菲,1993-01-14,女</span><br><span class="line">016,周冬雨,1990-06-18,女</span><br><span class="line">017,范冰冰,1992-07-04,女</span><br><span class="line">018,李冰冰,1993-09-24,女</span><br><span class="line">019,邓紫棋,1994-08-31,女</span><br><span class="line">020,宋丹丹,1991-03-01,女</span><br></pre></td></tr></table></figure>

<p><strong>course_info.txt</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">01,语文,1003</span><br><span class="line">02,数学,1001</span><br><span class="line">03,英语,1004</span><br><span class="line">04,体育,1002</span><br><span class="line">05,音乐,1002</span><br></pre></td></tr></table></figure>

<p><strong>teacher_info.txt</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1001,张高数</span><br><span class="line">1002,李体音</span><br><span class="line">1003,王子文</span><br><span class="line">1004,刘丽英</span><br></pre></td></tr></table></figure>

<p><strong>score_info.txt</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">001,01,94</span><br><span class="line">002,01,74</span><br><span class="line">004,01,85</span><br><span class="line">005,01,64</span><br><span class="line">006,01,71</span><br><span class="line">007,01,48</span><br><span class="line">008,01,56</span><br><span class="line">009,01,75</span><br><span class="line">010,01,84</span><br><span class="line">011,01,61</span><br><span class="line">012,01,44</span><br><span class="line">013,01,47</span><br><span class="line">014,01,81</span><br><span class="line">015,01,90</span><br><span class="line">016,01,71</span><br><span class="line">017,01,58</span><br><span class="line">018,01,38</span><br><span class="line">019,01,46</span><br><span class="line">020,01,89</span><br><span class="line">001,02,63</span><br><span class="line">002,02,84</span><br><span class="line">004,02,93</span><br><span class="line">005,02,44</span><br><span class="line">006,02,90</span><br><span class="line">007,02,55</span><br><span class="line">008,02,34</span><br><span class="line">009,02,78</span><br><span class="line">010,02,68</span><br><span class="line">011,02,49</span><br><span class="line">012,02,74</span><br><span class="line">013,02,35</span><br><span class="line">014,02,39</span><br><span class="line">015,02,48</span><br><span class="line">016,02,89</span><br><span class="line">017,02,34</span><br><span class="line">018,02,58</span><br><span class="line">019,02,39</span><br><span class="line">020,02,59</span><br><span class="line">001,03,79</span><br><span class="line">002,03,87</span><br><span class="line">004,03,89</span><br><span class="line">005,03,99</span><br><span class="line">006,03,59</span><br><span class="line">007,03,70</span><br><span class="line">008,03,39</span><br><span class="line">009,03,60</span><br><span class="line">010,03,47</span><br><span class="line">011,03,70</span><br><span class="line">012,03,62</span><br><span class="line">013,03,93</span><br><span class="line">014,03,32</span><br><span class="line">015,03,84</span><br><span class="line">016,03,71</span><br><span class="line">017,03,55</span><br><span class="line">018,03,49</span><br><span class="line">019,03,93</span><br><span class="line">020,03,81</span><br><span class="line">001,04,54</span><br><span class="line">002,04,100</span><br><span class="line">004,04,59</span><br><span class="line">005,04,85</span><br><span class="line">007,04,63</span><br><span class="line">009,04,79</span><br><span class="line">010,04,34</span><br><span class="line">013,04,69</span><br><span class="line">014,04,40</span><br><span class="line">016,04,94</span><br><span class="line">017,04,34</span><br><span class="line">020,04,50</span><br><span class="line">005,05,85</span><br><span class="line">007,05,63</span><br><span class="line">009,05,79</span><br><span class="line">015,05,59</span><br><span class="line">018,05,87</span><br></pre></td></tr></table></figure>

<h3 id="导入数据"><a href="#导入数据" class="headerlink" title="导入数据"></a>导入数据</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">load data <span class="keyword">local</span> inpath <span class="string">&#x27;/opt/module/hive-3.1.3/datas/student_info.txt&#x27;</span> <span class="keyword">into</span> <span class="keyword">table</span> student_info;</span><br><span class="line">load data <span class="keyword">local</span> inpath <span class="string">&#x27;/opt/module/hive-3.1.3/datas/course_info.txt&#x27;</span> <span class="keyword">into</span> <span class="keyword">table</span> course_info;</span><br><span class="line">load data <span class="keyword">local</span> inpath <span class="string">&#x27;/opt/module/hive-3.1.3/datas/teacher_info.txt&#x27;</span> <span class="keyword">into</span> <span class="keyword">table</span> teacher_info;</span><br><span class="line">load data <span class="keyword">local</span> inpath <span class="string">&#x27;/opt/module/hive-3.1.3/datas/score_info.txt&#x27;</span> <span class="keyword">into</span> <span class="keyword">table</span> score_info;</span><br></pre></td></tr></table></figure>

<h2 id="二、简单查询"><a href="#二、简单查询" class="headerlink" title="二、简单查询"></a>二、简单查询</h2><ol>
<li><strong>查询姓名中带“冰”的学生名单</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> student_info si <span class="keyword">where</span> si.stu_name <span class="keyword">like</span> <span class="string">&#x27;%冰%&#x27;</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span>birthday  <span class="operator">|</span>sex<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+----------+---+</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span><span class="number">1992</span><span class="number">-07</span><span class="number">-04</span><span class="operator">|</span>女  <span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span><span class="number">1993</span><span class="number">-09</span><span class="number">-24</span><span class="operator">|</span>女  <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查询姓“王”老师的个数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>) cnt <span class="keyword">from</span> teacher_info ti <span class="keyword">where</span> ti.tea_name <span class="keyword">like</span> <span class="string">&#x27;王%&#x27;</span>;</span><br><span class="line"></span><br><span class="line">cnt<span class="operator">|</span></span><br><span class="line"><span class="comment">---+</span></span><br><span class="line">  <span class="number">1</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>检索课程编号为“04”且分数小于60的学生的课程信息，结果按分数降序排列</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> stu.stu_id, stu.stu_name, stu.birthday, stu.sex, si.course_id, ci.course_name, si.score </span><br><span class="line"><span class="keyword">from</span> course_info ci</span><br><span class="line"><span class="keyword">join</span> score_info si <span class="keyword">on</span> ci.course_id <span class="operator">=</span> si.course_id </span><br><span class="line"><span class="keyword">join</span> student_info stu <span class="keyword">on</span> si.stu_id <span class="operator">=</span> stu.stu_id </span><br><span class="line"><span class="keyword">where</span> ci.course_id <span class="operator">=</span> <span class="string">&#x27;04&#x27;</span> <span class="keyword">and</span> si.score <span class="operator">&lt;</span> <span class="number">60</span></span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> si.score <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span>birthday  <span class="operator">|</span>sex<span class="operator">|</span>course_id<span class="operator">|</span>course_name<span class="operator">|</span>score<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+----------+---+---------+-----------+-----+</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span><span class="number">1998</span><span class="number">-08</span><span class="number">-28</span><span class="operator">|</span>男  <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>体育         <span class="operator">|</span>   <span class="number">59</span><span class="operator">|</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span><span class="number">1995</span><span class="number">-05</span><span class="number">-16</span><span class="operator">|</span>男  <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>体育         <span class="operator">|</span>   <span class="number">54</span><span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>宋丹丹     <span class="operator">|</span><span class="number">1991</span><span class="number">-03</span><span class="number">-01</span><span class="operator">|</span>女  <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>体育         <span class="operator">|</span>   <span class="number">50</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>赵丽颖     <span class="operator">|</span><span class="number">1990</span><span class="number">-01</span><span class="number">-09</span><span class="operator">|</span>女  <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>体育         <span class="operator">|</span>   <span class="number">40</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span><span class="number">1992</span><span class="number">-07</span><span class="number">-04</span><span class="operator">|</span>女  <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>体育         <span class="operator">|</span>   <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>于谦      <span class="operator">|</span><span class="number">1998</span><span class="number">-08</span><span class="number">-23</span><span class="operator">|</span>男  <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>体育         <span class="operator">|</span>   <span class="number">34</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>查询数学成绩不及格的学生和其对应的成绩，按照学号升序排序</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span>score<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+-----+</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>宋丹丹     <span class="operator">|</span>   <span class="number">59</span><span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>宋丹丹     <span class="operator">|</span>   <span class="number">50</span><span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span>   <span class="number">46</span><span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span>   <span class="number">39</span><span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span>   <span class="number">38</span><span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span>   <span class="number">49</span><span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span>   <span class="number">58</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span>   <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span>   <span class="number">58</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span>   <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span>   <span class="number">55</span><span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>刘亦菲     <span class="operator">|</span>   <span class="number">59</span><span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>刘亦菲     <span class="operator">|</span>   <span class="number">48</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>赵丽颖     <span class="operator">|</span>   <span class="number">40</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>赵丽颖     <span class="operator">|</span>   <span class="number">32</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>赵丽颖     <span class="operator">|</span>   <span class="number">39</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>蒋欣      <span class="operator">|</span>   <span class="number">35</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>蒋欣      <span class="operator">|</span>   <span class="number">47</span><span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>杨紫      <span class="operator">|</span>   <span class="number">44</span><span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>潘长江     <span class="operator">|</span>   <span class="number">49</span><span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>于谦      <span class="operator">|</span>   <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>于谦      <span class="operator">|</span>   <span class="number">47</span><span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span>   <span class="number">56</span><span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span>   <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span>   <span class="number">39</span><span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>陈坤      <span class="operator">|</span>   <span class="number">55</span><span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>陈坤      <span class="operator">|</span>   <span class="number">48</span><span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span>   <span class="number">59</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span>   <span class="number">44</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span>   <span class="number">59</span><span class="operator">|</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span>   <span class="number">54</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h2 id="三、汇总分析"><a href="#三、汇总分析" class="headerlink" title="三、汇总分析"></a>三、汇总分析</h2><h3 id="汇总分析"><a href="#汇总分析" class="headerlink" title="汇总分析"></a>汇总分析</h3><ol>
<li><strong>查询编号为“02”的课程的总成绩</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.course_id, <span class="built_in">sum</span>(si.score) score_sum</span><br><span class="line"><span class="keyword">from</span> score_info si  </span><br><span class="line"><span class="keyword">where</span> si.course_id <span class="operator">=</span> <span class="string">&#x27;02&#x27;</span></span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.course_id;</span><br><span class="line"></span><br><span class="line">course_id<span class="operator">|</span>score_sum<span class="operator">|</span></span><br><span class="line"><span class="comment">---------+---------+</span></span><br><span class="line"><span class="number">02</span>       <span class="operator">|</span>     <span class="number">1133</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查询参加考试的学生个数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(<span class="keyword">distinct</span> si.stu_id) cnt</span><br><span class="line"><span class="keyword">from</span> score_info si;</span><br><span class="line"></span><br><span class="line">cnt<span class="operator">|</span></span><br><span class="line"><span class="comment">---+</span></span><br><span class="line"> <span class="number">19</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h3 id="分组"><a href="#分组" class="headerlink" title="分组"></a>分组</h3><ol>
<li><strong>查询各科成绩最高和最低的分，以如下的形式显示：课程号，最高分，最低分</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.course_id, <span class="built_in">max</span>(si.score) max_socre, <span class="built_in">min</span>(si.score) min_socre</span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.course_id;</span><br><span class="line"></span><br><span class="line">course_id<span class="operator">|</span>max_socre<span class="operator">|</span>min_socre<span class="operator">|</span></span><br><span class="line"><span class="comment">---------+---------+---------+</span></span><br><span class="line"><span class="number">01</span>       <span class="operator">|</span>       <span class="number">94</span><span class="operator">|</span>       <span class="number">38</span><span class="operator">|</span></span><br><span class="line"><span class="number">02</span>       <span class="operator">|</span>       <span class="number">93</span><span class="operator">|</span>       <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">03</span>       <span class="operator">|</span>       <span class="number">99</span><span class="operator">|</span>       <span class="number">32</span><span class="operator">|</span></span><br><span class="line"><span class="number">04</span>       <span class="operator">|</span>      <span class="number">100</span><span class="operator">|</span>       <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">05</span>       <span class="operator">|</span>       <span class="number">87</span><span class="operator">|</span>       <span class="number">59</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查询每门课程有多少学生参加了考试（有考试成绩）</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.course_id, <span class="built_in">count</span>(si.stu_id) stu_cnt</span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.course_id;</span><br><span class="line"></span><br><span class="line">course_id<span class="operator">|</span>stu_cnt<span class="operator">|</span></span><br><span class="line"><span class="comment">---------+-------+</span></span><br><span class="line"><span class="number">01</span>       <span class="operator">|</span>     <span class="number">19</span><span class="operator">|</span></span><br><span class="line"><span class="number">02</span>       <span class="operator">|</span>     <span class="number">19</span><span class="operator">|</span></span><br><span class="line"><span class="number">03</span>       <span class="operator">|</span>     <span class="number">19</span><span class="operator">|</span></span><br><span class="line"><span class="number">04</span>       <span class="operator">|</span>     <span class="number">12</span><span class="operator">|</span></span><br><span class="line"><span class="number">05</span>       <span class="operator">|</span>      <span class="number">5</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>查询男生、女生人数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.sex, <span class="built_in">count</span>(si.stu_id) cnt</span><br><span class="line"><span class="keyword">from</span> student_info si </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.sex;</span><br><span class="line"></span><br><span class="line">sex<span class="operator">|</span>cnt<span class="operator">|</span></span><br><span class="line"><span class="comment">---+---+</span></span><br><span class="line">女  <span class="operator">|</span>  <span class="number">9</span><span class="operator">|</span></span><br><span class="line">男  <span class="operator">|</span> <span class="number">11</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h3 id="分组结果的条件"><a href="#分组结果的条件" class="headerlink" title="分组结果的条件"></a>分组结果的条件</h3><ol>
<li><strong>查询平均成绩大于60分的学生的学号和平均成绩</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.stu_id, <span class="built_in">avg</span>(si2.score) avg_score</span><br><span class="line"><span class="keyword">from</span> student_info si </span><br><span class="line"><span class="keyword">join</span> score_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.stu_id </span><br><span class="line"><span class="keyword">having</span> <span class="built_in">avg</span>(si2.score) <span class="operator">&gt;</span> <span class="number">60</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>avg_score        <span class="operator">|</span></span><br><span class="line"><span class="comment">------+-----------------+</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>             <span class="number">72.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>            <span class="number">86.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>             <span class="number">81.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>             <span class="number">75.4</span><span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span><span class="number">73.33333333333333</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>             <span class="number">74.2</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>             <span class="number">61.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>            <span class="number">70.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>            <span class="number">81.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>            <span class="number">69.75</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查询至少选修四门课程的学生学号</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.stu_id, <span class="built_in">count</span>(si2.course_id) cnt_core</span><br><span class="line"><span class="keyword">from</span> student_info si </span><br><span class="line"><span class="keyword">join</span> score_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2 .stu_id </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.stu_id  </span><br><span class="line"><span class="keyword">having</span> <span class="built_in">count</span>(si2.course_id) <span class="operator">&gt;=</span> <span class="number">4</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>cnt_core<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>       <span class="number">5</span><span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>       <span class="number">5</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>       <span class="number">5</span><span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>       <span class="number">4</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li>**查询同姓（假设每个学生姓名的第一个字为姓）的学生名单并统计同姓人数大于等于2的姓 **</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> substr(si.stu_name, <span class="number">0</span>, <span class="number">1</span>) first_name, <span class="built_in">count</span>(<span class="operator">*</span>) cnt</span><br><span class="line"><span class="keyword">from</span> student_info si </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> substr(si.stu_name, <span class="number">0</span>, <span class="number">1</span>)</span><br><span class="line"><span class="keyword">having</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="operator">&gt;=</span> <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">first_name<span class="operator">|</span>cnt<span class="operator">|</span></span><br><span class="line"><span class="comment">----------+---+</span></span><br><span class="line">刘         <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">周         <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">陈         <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>查询每门课程的平均成绩，结果按平均成绩升序排序，平均成绩相同时，按课程号降序排列</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.course_id, <span class="built_in">avg</span>(si.score) avg_score</span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.course_id </span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> avg_score, si.course_id <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line">course_id<span class="operator">|</span>avg_score         <span class="operator">|</span></span><br><span class="line"><span class="comment">---------+------------------+</span></span><br><span class="line"><span class="number">02</span>       <span class="operator">|</span> <span class="number">59.63157894736842</span><span class="operator">|</span></span><br><span class="line"><span class="number">04</span>       <span class="operator">|</span><span class="number">63.416666666666664</span><span class="operator">|</span></span><br><span class="line"><span class="number">01</span>       <span class="operator">|</span> <span class="number">67.15789473684211</span><span class="operator">|</span></span><br><span class="line"><span class="number">03</span>       <span class="operator">|</span> <span class="number">69.42105263157895</span><span class="operator">|</span></span><br><span class="line"><span class="number">05</span>       <span class="operator">|</span>              <span class="number">74.6</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="5">
<li><strong>统计参加考试人数大于等于15的学科</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.course_id, <span class="built_in">count</span>(si.stu_id) cnt_stu</span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.course_id </span><br><span class="line"><span class="keyword">HAVING</span> cnt_stu <span class="operator">&gt;</span> <span class="number">15</span>;</span><br><span class="line"></span><br><span class="line">course_id<span class="operator">|</span>cnt_stu<span class="operator">|</span></span><br><span class="line"><span class="comment">---------+-------+</span></span><br><span class="line"><span class="number">01</span>       <span class="operator">|</span>     <span class="number">19</span><span class="operator">|</span></span><br><span class="line"><span class="number">02</span>       <span class="operator">|</span>     <span class="number">19</span><span class="operator">|</span></span><br><span class="line"><span class="number">03</span>       <span class="operator">|</span>     <span class="number">19</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h3 id="查询结果排序-amp-分组指定条件"><a href="#查询结果排序-amp-分组指定条件" class="headerlink" title="查询结果排序 &amp; 分组指定条件"></a>查询结果排序 &amp; 分组指定条件</h3><ol>
<li><strong>查询学生的总成绩并按照总成绩降序排序</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.stu_id, <span class="built_in">sum</span>(si.score) sum_score</span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.stu_id </span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> sum_score <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>sum_score<span class="operator">|</span></span><br><span class="line"><span class="comment">------+---------+</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>      <span class="number">377</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>      <span class="number">371</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>      <span class="number">345</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>      <span class="number">326</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>      <span class="number">325</span><span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>      <span class="number">299</span><span class="operator">|</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>      <span class="number">290</span><span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>      <span class="number">281</span><span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>      <span class="number">279</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>      <span class="number">244</span><span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>      <span class="number">233</span><span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>      <span class="number">232</span><span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>      <span class="number">220</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>      <span class="number">192</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>      <span class="number">181</span><span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>      <span class="number">180</span><span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>      <span class="number">180</span><span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>      <span class="number">178</span><span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>      <span class="number">129</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>**按照如下格式显示学生的语文、数学、英语三科成绩，没有成绩的输出为0，按照学生的有效平均成绩降序显示 **</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	si.stu_id,</span><br><span class="line">	<span class="built_in">sum</span>(if(ci.course_name<span class="operator">=</span><span class="string">&#x27;语文&#x27;</span>, si.score, <span class="number">0</span>)) chinese,</span><br><span class="line">	<span class="built_in">sum</span>(if(ci.course_name<span class="operator">=</span><span class="string">&#x27;数学&#x27;</span>, si.score, <span class="number">0</span>)) math,</span><br><span class="line">	<span class="built_in">sum</span>(if(ci.course_name<span class="operator">=</span><span class="string">&#x27;英语&#x27;</span>, si.score, <span class="number">0</span>)) english,</span><br><span class="line">	<span class="built_in">count</span>(<span class="operator">*</span>) cnt_course,</span><br><span class="line">	<span class="built_in">avg</span>(si.score) avg_score</span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">join</span> course_info ci <span class="keyword">on</span> si.course_id <span class="operator">=</span> ci.course_id </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.stu_id </span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> avg_score <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>chinese<span class="operator">|</span>math<span class="operator">|</span>english<span class="operator">|</span>cnt_course<span class="operator">|</span>avg_score         <span class="operator">|</span></span><br><span class="line"><span class="comment">------+-------+----+-------+----------+------------------+</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>     <span class="number">74</span><span class="operator">|</span>  <span class="number">84</span><span class="operator">|</span>     <span class="number">87</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>             <span class="number">86.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>     <span class="number">85</span><span class="operator">|</span>  <span class="number">93</span><span class="operator">|</span>     <span class="number">89</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>              <span class="number">81.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>     <span class="number">71</span><span class="operator">|</span>  <span class="number">89</span><span class="operator">|</span>     <span class="number">71</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>             <span class="number">81.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>     <span class="number">64</span><span class="operator">|</span>  <span class="number">44</span><span class="operator">|</span>     <span class="number">99</span><span class="operator">|</span>         <span class="number">5</span><span class="operator">|</span>              <span class="number">75.4</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>     <span class="number">75</span><span class="operator">|</span>  <span class="number">78</span><span class="operator">|</span>     <span class="number">60</span><span class="operator">|</span>         <span class="number">5</span><span class="operator">|</span>              <span class="number">74.2</span><span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>     <span class="number">71</span><span class="operator">|</span>  <span class="number">90</span><span class="operator">|</span>     <span class="number">59</span><span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span> <span class="number">73.33333333333333</span><span class="operator">|</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>     <span class="number">94</span><span class="operator">|</span>  <span class="number">63</span><span class="operator">|</span>     <span class="number">79</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>              <span class="number">72.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>     <span class="number">90</span><span class="operator">|</span>  <span class="number">48</span><span class="operator">|</span>     <span class="number">84</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>             <span class="number">70.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>     <span class="number">89</span><span class="operator">|</span>  <span class="number">59</span><span class="operator">|</span>     <span class="number">81</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>             <span class="number">69.75</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>     <span class="number">47</span><span class="operator">|</span>  <span class="number">35</span><span class="operator">|</span>     <span class="number">93</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>              <span class="number">61.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>     <span class="number">44</span><span class="operator">|</span>  <span class="number">74</span><span class="operator">|</span>     <span class="number">62</span><span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span>              <span class="number">60.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>     <span class="number">61</span><span class="operator">|</span>  <span class="number">49</span><span class="operator">|</span>     <span class="number">70</span><span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span>              <span class="number">60.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>     <span class="number">48</span><span class="operator">|</span>  <span class="number">55</span><span class="operator">|</span>     <span class="number">70</span><span class="operator">|</span>         <span class="number">5</span><span class="operator">|</span>              <span class="number">59.8</span><span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>     <span class="number">46</span><span class="operator">|</span>  <span class="number">39</span><span class="operator">|</span>     <span class="number">93</span><span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span><span class="number">59.333333333333336</span><span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>     <span class="number">84</span><span class="operator">|</span>  <span class="number">68</span><span class="operator">|</span>     <span class="number">47</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>             <span class="number">58.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>     <span class="number">38</span><span class="operator">|</span>  <span class="number">58</span><span class="operator">|</span>     <span class="number">49</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>              <span class="number">58.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>     <span class="number">81</span><span class="operator">|</span>  <span class="number">39</span><span class="operator">|</span>     <span class="number">32</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>              <span class="number">48.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>     <span class="number">58</span><span class="operator">|</span>  <span class="number">34</span><span class="operator">|</span>     <span class="number">55</span><span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>             <span class="number">45.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>     <span class="number">56</span><span class="operator">|</span>  <span class="number">34</span><span class="operator">|</span>     <span class="number">39</span><span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span>              <span class="number">43.0</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>查询一共参加三门课程且其中一门为语文课程的学生的id和姓名</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> t2.stu_id, si3.stu_name </span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> t1.stu_id, <span class="built_in">count</span>(t1.course_id) cnt</span><br><span class="line">	<span class="keyword">from</span> (</span><br><span class="line">		<span class="keyword">select</span> si2.stu_id, si2.course_id </span><br><span class="line">		<span class="keyword">from</span> score_info si2 </span><br><span class="line">		<span class="keyword">where</span> si2.stu_id <span class="keyword">in</span> (</span><br><span class="line">			<span class="keyword">select</span> si.stu_id</span><br><span class="line">			<span class="keyword">from</span> course_info ci </span><br><span class="line">			<span class="keyword">join</span> score_info si <span class="keyword">on</span> ci .course_id <span class="operator">=</span> si.course_id </span><br><span class="line">			<span class="keyword">where</span> ci.course_name <span class="operator">=</span> <span class="string">&#x27;语文&#x27;</span></span><br><span class="line">		)</span><br><span class="line">	) t1</span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> t1.stu_id</span><br><span class="line">	<span class="keyword">having</span> <span class="built_in">count</span>(t1.course_id) <span class="operator">=</span> <span class="number">3</span></span><br><span class="line">) t2</span><br><span class="line"><span class="keyword">join</span> student_info si3 <span class="keyword">on</span> t2.stu_id <span class="operator">=</span> si3.stu_id ;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>潘长江     <span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>杨紫      <span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h2 id="四、复杂查询"><a href="#四、复杂查询" class="headerlink" title="四、复杂查询"></a>四、复杂查询</h2><h3 id="子查询"><a href="#子查询" class="headerlink" title="子查询"></a>子查询</h3><ol>
<li>**查询所有课程成绩均小于60分的学生的学号、姓名 **</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si2.stu_id, si2.stu_name</span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id, <span class="built_in">sum</span>(if(si.score <span class="operator">&gt;=</span> <span class="number">60</span>, <span class="number">1</span>, <span class="number">0</span>)) flag</span><br><span class="line">	<span class="keyword">from</span> score_info si </span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> si.stu_id </span><br><span class="line">	<span class="keyword">having</span> flag <span class="operator">=</span> <span class="number">0</span></span><br><span class="line">) t1</span><br><span class="line"><span class="keyword">join</span> student_info si2 <span class="keyword">on</span> t1.stu_id <span class="operator">=</span> si2.stu_id; </span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查询没有学全所有课的学生的学号、姓名</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.stu_id, si.stu_name </span><br><span class="line"><span class="keyword">from</span> student_info si </span><br><span class="line"><span class="keyword">left</span> <span class="keyword">join</span> score_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.stu_id, si.stu_name </span><br><span class="line"><span class="keyword">having</span> <span class="built_in">count</span>(si2.course_id) <span class="operator">&lt;</span> (</span><br><span class="line">	<span class="keyword">select</span> <span class="built_in">count</span>(ci.course_id) <span class="keyword">from</span> course_info ci </span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span></span><br><span class="line"><span class="number">003</span>   <span class="operator">|</span>周杰伦     <span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>于谦      <span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>潘长江     <span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>杨紫      <span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>蒋欣      <span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>赵丽颖     <span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>刘亦菲     <span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>宋丹丹     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>查询出只选修了三门课程的全部学生的学号和姓名</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.stu_id, si.stu_name </span><br><span class="line"><span class="keyword">from</span> student_info si </span><br><span class="line"><span class="keyword">left</span> <span class="keyword">join</span> score_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.stu_id, si.stu_name </span><br><span class="line"><span class="keyword">having</span> <span class="built_in">count</span>(si2.course_id)  <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>潘长江     <span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>杨紫      <span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h2 id="五、多表查询"><a href="#五、多表查询" class="headerlink" title="五、多表查询"></a>五、多表查询</h2><h3 id="表连接"><a href="#表连接" class="headerlink" title="表连接"></a>表连接</h3><ol>
<li><strong>查询有两门以上的课程不及格的同学的学号及其平均成绩</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> t1.stu_id, t2.avg_score</span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id, <span class="built_in">sum</span>(if(si.score <span class="operator">&lt;</span> <span class="number">60</span>, <span class="number">1</span>, <span class="number">0</span>)) flag</span><br><span class="line">	<span class="keyword">from</span> score_info si </span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> si.stu_id </span><br><span class="line">	<span class="keyword">having</span> flag <span class="operator">&gt;=</span> <span class="number">2</span></span><br><span class="line">) t1</span><br><span class="line"><span class="keyword">join</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id, <span class="built_in">avg</span>(si.score) avg_score</span><br><span class="line">	<span class="keyword">from</span> score_info si </span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> si.stu_id </span><br><span class="line">) t2 <span class="keyword">on</span> t1.stu_id <span class="operator">=</span> t2.stu_id;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>avg_score         <span class="operator">|</span></span><br><span class="line"><span class="comment">------+------------------+</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>              <span class="number">59.8</span><span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>              <span class="number">43.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>             <span class="number">58.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>              <span class="number">61.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>              <span class="number">48.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>             <span class="number">70.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>             <span class="number">45.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>              <span class="number">58.0</span><span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span><span class="number">59.333333333333336</span><span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>             <span class="number">69.75</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查询所有学生的学号、姓名、选课数、总成绩</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.stu_id, si.stu_name, <span class="built_in">count</span>(si2.course_id) cnt_course, <span class="built_in">sum</span>(si2.score) sum_score</span><br><span class="line"><span class="keyword">from</span> student_info si </span><br><span class="line"><span class="keyword">join</span> score_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.stu_id, si.stu_name;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span>cnt_course<span class="operator">|</span>sum_score<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+----------+---------+</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">290</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">345</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">326</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span>         <span class="number">5</span><span class="operator">|</span>      <span class="number">377</span><span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span>      <span class="number">220</span><span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>陈坤      <span class="operator">|</span>         <span class="number">5</span><span class="operator">|</span>      <span class="number">299</span><span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span>      <span class="number">129</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span>         <span class="number">5</span><span class="operator">|</span>      <span class="number">371</span><span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>于谦      <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">233</span><span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>潘长江     <span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span>      <span class="number">180</span><span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>杨紫      <span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span>      <span class="number">180</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>蒋欣      <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">244</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>赵丽颖     <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">192</span><span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>刘亦菲     <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">281</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">325</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">181</span><span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">232</span><span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span>         <span class="number">3</span><span class="operator">|</span>      <span class="number">178</span><span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>宋丹丹     <span class="operator">|</span>         <span class="number">4</span><span class="operator">|</span>      <span class="number">279</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>查询平均成绩大于85的所有学生的学号、姓名和平均成绩</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.stu_id, si.stu_name, <span class="built_in">avg</span>(si2.score) avg_score</span><br><span class="line"><span class="keyword">from</span> student_info si </span><br><span class="line"><span class="keyword">join</span> score_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si.stu_id, si.stu_name </span><br><span class="line"><span class="keyword">having</span> avg_score <span class="operator">&gt;</span> <span class="number">85</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span>avg_score<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+---------+</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>    <span class="number">86.25</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>查询出每门课程的及格人数和不及格人数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> ci.course_id, ci.course_name, t1.cnt_pass, t1.cnt_fail </span><br><span class="line"><span class="keyword">from</span> course_info ci </span><br><span class="line"><span class="keyword">join</span> (</span><br><span class="line">	<span class="keyword">select</span> </span><br><span class="line">		si.course_id, </span><br><span class="line">		<span class="built_in">sum</span>(if(si.score <span class="operator">&gt;=</span> <span class="number">60</span>, <span class="number">1</span>, <span class="number">0</span>)) cnt_pass,</span><br><span class="line">		<span class="built_in">sum</span>(if(si.score <span class="operator">&lt;</span> <span class="number">60</span>, <span class="number">1</span>, <span class="number">0</span>)) cnt_fail</span><br><span class="line">	<span class="keyword">from</span> score_info si </span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> si.course_id </span><br><span class="line">) t1 <span class="keyword">on</span> ci.course_id <span class="operator">=</span> t1.course_id;</span><br><span class="line"></span><br><span class="line">course_id<span class="operator">|</span>course_name<span class="operator">|</span>pass<span class="operator">|</span>fail<span class="operator">|</span></span><br><span class="line"><span class="comment">---------+-----------+----+----+</span></span><br><span class="line"><span class="number">01</span>       <span class="operator">|</span>语文         <span class="operator">|</span>  <span class="number">12</span><span class="operator">|</span>   <span class="number">7</span><span class="operator">|</span></span><br><span class="line"><span class="number">02</span>       <span class="operator">|</span>数学         <span class="operator">|</span>   <span class="number">8</span><span class="operator">|</span>  <span class="number">11</span><span class="operator">|</span></span><br><span class="line"><span class="number">03</span>       <span class="operator">|</span>英语         <span class="operator">|</span>  <span class="number">13</span><span class="operator">|</span>   <span class="number">6</span><span class="operator">|</span></span><br><span class="line"><span class="number">04</span>       <span class="operator">|</span>体育         <span class="operator">|</span>   <span class="number">6</span><span class="operator">|</span>   <span class="number">6</span><span class="operator">|</span></span><br><span class="line"><span class="number">05</span>       <span class="operator">|</span>音乐         <span class="operator">|</span>   <span class="number">4</span><span class="operator">|</span>   <span class="number">1</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="5">
<li><strong>查询课程编号为03且课程成绩在80分以上的学生的学号和姓名及课程信息</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si2.stu_id, si2.stu_name, si.course_id, ci.course_name </span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">join</span> student_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line"><span class="keyword">join</span> course_info ci <span class="keyword">on</span> si.course_id <span class="operator">=</span> ci.course_id </span><br><span class="line"><span class="keyword">where</span> si.course_id <span class="operator">=</span> <span class="string">&#x27;03&#x27;</span> <span class="keyword">and</span> si.score <span class="operator">&gt;</span> <span class="number">80</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span>course_id<span class="operator">|</span>course_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+---------+-----------+</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span><span class="number">03</span>       <span class="operator">|</span>英语         <span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span><span class="number">03</span>       <span class="operator">|</span>英语         <span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span><span class="number">03</span>       <span class="operator">|</span>英语         <span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>蒋欣      <span class="operator">|</span><span class="number">03</span>       <span class="operator">|</span>英语         <span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>刘亦菲     <span class="operator">|</span><span class="number">03</span>       <span class="operator">|</span>英语         <span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span><span class="number">03</span>       <span class="operator">|</span>英语         <span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>宋丹丹     <span class="operator">|</span><span class="number">03</span>       <span class="operator">|</span>英语         <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h3 id="多表连接-1"><a href="#多表连接-1" class="headerlink" title="多表连接"></a>多表连接</h3><ol>
<li><strong>课程编号为”01”且课程分数小于60，按分数降序排列的学生信息</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si2.stu_id, si2.stu_name, si2.birthday, si2.sex, si.course_id, si.score </span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">join</span> student_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line"><span class="keyword">where</span> si.course_id <span class="operator">=</span> <span class="string">&#x27;01&#x27;</span> <span class="keyword">and</span> si.score  <span class="operator">&lt;</span> <span class="number">60</span></span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> si.score <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span>birthday  <span class="operator">|</span>sex<span class="operator">|</span>course_id<span class="operator">|</span>score<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+----------+---+---------+-----+</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span><span class="number">1992</span><span class="number">-07</span><span class="number">-04</span><span class="operator">|</span>女  <span class="operator">|</span><span class="number">01</span>       <span class="operator">|</span>   <span class="number">58</span><span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span><span class="number">1994</span><span class="number">-02</span><span class="number">-06</span><span class="operator">|</span>男  <span class="operator">|</span><span class="number">01</span>       <span class="operator">|</span>   <span class="number">56</span><span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>陈坤      <span class="operator">|</span><span class="number">1999</span><span class="number">-04</span><span class="number">-09</span><span class="operator">|</span>男  <span class="operator">|</span><span class="number">01</span>       <span class="operator">|</span>   <span class="number">48</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>蒋欣      <span class="operator">|</span><span class="number">1997</span><span class="number">-11</span><span class="number">-08</span><span class="operator">|</span>女  <span class="operator">|</span><span class="number">01</span>       <span class="operator">|</span>   <span class="number">47</span><span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span><span class="number">1994</span><span class="number">-08</span><span class="number">-31</span><span class="operator">|</span>女  <span class="operator">|</span><span class="number">01</span>       <span class="operator">|</span>   <span class="number">46</span><span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>杨紫      <span class="operator">|</span><span class="number">1996</span><span class="number">-12</span><span class="number">-21</span><span class="operator">|</span>女  <span class="operator">|</span><span class="number">01</span>       <span class="operator">|</span>   <span class="number">44</span><span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span><span class="number">1993</span><span class="number">-09</span><span class="number">-24</span><span class="operator">|</span>女  <span class="operator">|</span><span class="number">01</span>       <span class="operator">|</span>   <span class="number">38</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查询所有课程成绩在70分以上的学生的姓名、课程名称和分数，按分数升序排列</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si3.stu_id, si3.stu_name, ci.course_name, si2.score </span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id, <span class="built_in">sum</span>(if(si.score <span class="operator">&gt;</span> <span class="number">70</span>, <span class="number">0</span>, <span class="number">1</span>)) flag</span><br><span class="line">	<span class="keyword">from</span> score_info si </span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> si.stu_id </span><br><span class="line">	<span class="keyword">having</span> flag <span class="operator">=</span> <span class="number">0</span></span><br><span class="line">) t1</span><br><span class="line"><span class="keyword">join</span> score_info si2 <span class="keyword">on</span> t1.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line"><span class="keyword">join</span> student_info si3 <span class="keyword">on</span> si2.stu_id <span class="operator">=</span> si3.stu_id </span><br><span class="line"><span class="keyword">join</span> course_info ci <span class="keyword">on</span> si2.course_id <span class="operator">=</span> ci.course_id </span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> si2.score;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span>course_name<span class="operator">|</span>score<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+-----------+-----+</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span>英语         <span class="operator">|</span>   <span class="number">71</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span>语文         <span class="operator">|</span>   <span class="number">71</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>语文         <span class="operator">|</span>   <span class="number">74</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>数学         <span class="operator">|</span>   <span class="number">84</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>英语         <span class="operator">|</span>   <span class="number">87</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span>数学         <span class="operator">|</span>   <span class="number">89</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span>体育         <span class="operator">|</span>   <span class="number">94</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>体育         <span class="operator">|</span>  <span class="number">100</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>查询该学生不同课程的成绩相同的学生编号、课程编号、学生成绩</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.stu_id, si.course_id, si.score </span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">join</span> score_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line">	<span class="keyword">and</span> si.course_id  <span class="operator">&lt;&gt;</span> si2.course_id </span><br><span class="line">	<span class="keyword">and</span> si.score <span class="operator">=</span> si2.score ;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>course_id<span class="operator">|</span>score<span class="operator">|</span></span><br><span class="line"><span class="comment">------+---------+-----+</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span><span class="number">03</span>       <span class="operator">|</span>   <span class="number">71</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>   <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span><span class="number">01</span>       <span class="operator">|</span>   <span class="number">71</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span><span class="number">05</span>       <span class="operator">|</span>   <span class="number">85</span><span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span><span class="number">05</span>       <span class="operator">|</span>   <span class="number">63</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span><span class="number">05</span>       <span class="operator">|</span>   <span class="number">79</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span><span class="number">02</span>       <span class="operator">|</span>   <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>   <span class="number">85</span><span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>   <span class="number">63</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span><span class="number">04</span>       <span class="operator">|</span>   <span class="number">79</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> t1.stu_id, t1.score <span class="number">01</span>_score, t2.score <span class="number">02</span>_score</span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id, si.score <span class="keyword">from</span> score_info si <span class="keyword">where</span> si.course_id <span class="operator">=</span> <span class="string">&#x27;01&#x27;</span></span><br><span class="line">) t1</span><br><span class="line"><span class="keyword">join</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id, si.score <span class="keyword">from</span> score_info si <span class="keyword">where</span> si.course_id <span class="operator">=</span> <span class="string">&#x27;02&#x27;</span></span><br><span class="line">) t2 <span class="keyword">on</span> t1.stu_id <span class="operator">=</span> t2.stu_id</span><br><span class="line"><span class="keyword">where</span> t1.score <span class="operator">&gt;</span> t2.score;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span><span class="number">01</span>_score<span class="operator">|</span><span class="number">02</span>_score<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+--------+</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>      <span class="number">94</span><span class="operator">|</span>      <span class="number">63</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>      <span class="number">64</span><span class="operator">|</span>      <span class="number">44</span><span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>      <span class="number">56</span><span class="operator">|</span>      <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>      <span class="number">84</span><span class="operator">|</span>      <span class="number">68</span><span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>      <span class="number">61</span><span class="operator">|</span>      <span class="number">49</span><span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>      <span class="number">47</span><span class="operator">|</span>      <span class="number">35</span><span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>      <span class="number">81</span><span class="operator">|</span>      <span class="number">39</span><span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>      <span class="number">90</span><span class="operator">|</span>      <span class="number">48</span><span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>      <span class="number">58</span><span class="operator">|</span>      <span class="number">34</span><span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>      <span class="number">46</span><span class="operator">|</span>      <span class="number">39</span><span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>      <span class="number">89</span><span class="operator">|</span>      <span class="number">59</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="5">
<li><strong>查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si2.stu_id, si2.stu_name </span><br><span class="line"><span class="keyword">from</span> student_info si2 </span><br><span class="line"><span class="keyword">join</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id, <span class="built_in">count</span>(si.course_id)</span><br><span class="line">	<span class="keyword">from</span> score_info si </span><br><span class="line">	<span class="keyword">where</span> si.course_id <span class="keyword">in</span> (<span class="string">&#x27;01&#x27;</span>, <span class="string">&#x27;02&#x27;</span>)</span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> si.stu_id </span><br><span class="line">	<span class="keyword">having</span> <span class="built_in">count</span>(si.course_id) <span class="operator">=</span> <span class="number">2</span></span><br><span class="line">) t1 <span class="keyword">on</span> si2.stu_id <span class="operator">=</span> t1.stu_id;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>陈坤      <span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>于谦      <span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>潘长江     <span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>杨紫      <span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>蒋欣      <span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>赵丽颖     <span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>刘亦菲     <span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>宋丹丹     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="6">
<li><strong>查询学过“李体音”老师所教的所有课的同学的学号、姓名</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si2.stu_id, si2.stu_name </span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id </span><br><span class="line">	<span class="keyword">from</span> score_info si </span><br><span class="line">	<span class="keyword">where</span> si.course_id <span class="keyword">in</span> (</span><br><span class="line">		<span class="keyword">select</span> ci.course_id </span><br><span class="line">		<span class="keyword">from</span> course_info ci </span><br><span class="line">		<span class="keyword">join</span> teacher_info ti <span class="keyword">on</span> ci.tea_id <span class="operator">=</span> ti.tea_id </span><br><span class="line">		<span class="keyword">where</span> ti.tea_name <span class="operator">=</span> <span class="string">&#x27;李体音&#x27;</span></span><br><span class="line">	)</span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> stu_id </span><br><span class="line">	<span class="keyword">having</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="operator">=</span> <span class="number">2</span></span><br><span class="line">) t1 </span><br><span class="line"><span class="keyword">join</span> student_info si2 <span class="keyword">on</span> t1.stu_id <span class="operator">=</span> si2.stu_id;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>陈坤      <span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="7">
<li><strong>查询学过“李体音”老师所讲授的任意一门课程的学生的学号、姓名</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si2.stu_id, si2.stu_name </span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id </span><br><span class="line">	<span class="keyword">from</span> score_info si </span><br><span class="line">	<span class="keyword">where</span> si.course_id <span class="keyword">in</span> (</span><br><span class="line">		<span class="keyword">select</span> ci.course_id </span><br><span class="line">		<span class="keyword">from</span> course_info ci </span><br><span class="line">		<span class="keyword">join</span> teacher_info ti <span class="keyword">on</span> ci.tea_id <span class="operator">=</span> ti.tea_id </span><br><span class="line">		<span class="keyword">where</span> ti.tea_name <span class="operator">=</span> <span class="string">&#x27;李体音&#x27;</span></span><br><span class="line">	)</span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> si.stu_id</span><br><span class="line">) t1 </span><br><span class="line"><span class="keyword">join</span> student_info si2 <span class="keyword">on</span> t1.stu_id <span class="operator">=</span> si2.stu_id;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>陈坤      <span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>于谦      <span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>蒋欣      <span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>赵丽颖     <span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>刘亦菲     <span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>宋丹丹     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="8">
<li>**查询没学过”李体音”老师讲授的任一门课程的学生姓名 **</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si2.stu_id, si2.stu_name </span><br><span class="line"><span class="keyword">from</span> student_info si2 </span><br><span class="line"><span class="keyword">where</span> si2.stu_id <span class="keyword">not</span> <span class="keyword">in</span> (</span><br><span class="line">	<span class="keyword">select</span> si.stu_id </span><br><span class="line">	<span class="keyword">from</span> score_info si </span><br><span class="line">	<span class="keyword">where</span> si.course_id <span class="keyword">in</span> (</span><br><span class="line">		<span class="keyword">select</span> ci.course_id </span><br><span class="line">		<span class="keyword">from</span> course_info ci </span><br><span class="line">		<span class="keyword">join</span> teacher_info ti <span class="keyword">on</span> ci.tea_id <span class="operator">=</span> ti.tea_id </span><br><span class="line">		<span class="keyword">where</span> ti.tea_name <span class="operator">=</span> <span class="string">&#x27;李体音&#x27;</span></span><br><span class="line">	)</span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> si.stu_id</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">003</span>   <span class="operator">|</span>周杰伦     <span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>潘长江     <span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>杨紫      <span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="9">
<li>**查询至少有一门课与学号为“001”的学生所学课程相同的学生的学号和姓名 **</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si3.stu_id, si3.stu_name </span><br><span class="line"><span class="keyword">from</span> score_info si2 </span><br><span class="line"><span class="keyword">join</span> student_info si3 <span class="keyword">on</span> si2.stu_id <span class="operator">=</span> si3.stu_id </span><br><span class="line"><span class="keyword">where</span> si2.course_id <span class="keyword">in</span> (</span><br><span class="line">	<span class="keyword">select</span> si.course_id  <span class="keyword">from</span> score_info si <span class="keyword">where</span> si.stu_id <span class="operator">=</span> <span class="string">&#x27;001&#x27;</span></span><br><span class="line">) <span class="keyword">and</span> si3.stu_id <span class="operator">&lt;&gt;</span> <span class="string">&#x27;001&#x27;</span></span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> si3.stu_id, si3.stu_name ;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span></span><br><span class="line"><span class="number">007</span>   <span class="operator">|</span>陈坤      <span class="operator">|</span></span><br><span class="line"><span class="number">008</span>   <span class="operator">|</span>吴京      <span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span></span><br><span class="line"><span class="number">010</span>   <span class="operator">|</span>于谦      <span class="operator">|</span></span><br><span class="line"><span class="number">011</span>   <span class="operator">|</span>潘长江     <span class="operator">|</span></span><br><span class="line"><span class="number">012</span>   <span class="operator">|</span>杨紫      <span class="operator">|</span></span><br><span class="line"><span class="number">013</span>   <span class="operator">|</span>蒋欣      <span class="operator">|</span></span><br><span class="line"><span class="number">014</span>   <span class="operator">|</span>赵丽颖     <span class="operator">|</span></span><br><span class="line"><span class="number">015</span>   <span class="operator">|</span>刘亦菲     <span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span></span><br><span class="line"><span class="number">017</span>   <span class="operator">|</span>范冰冰     <span class="operator">|</span></span><br><span class="line"><span class="number">018</span>   <span class="operator">|</span>李冰冰     <span class="operator">|</span></span><br><span class="line"><span class="number">019</span>   <span class="operator">|</span>邓紫棋     <span class="operator">|</span></span><br><span class="line"><span class="number">020</span>   <span class="operator">|</span>宋丹丹     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="10">
<li><strong>按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> si.stu_id, si2.stu_name, si.score, t1.avg_score</span><br><span class="line"><span class="keyword">from</span> score_info si </span><br><span class="line"><span class="keyword">join</span> student_info si2 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> si2.stu_id </span><br><span class="line"><span class="keyword">join</span> course_info ci <span class="keyword">on</span> si.course_id <span class="operator">=</span> ci.course_id </span><br><span class="line"><span class="keyword">join</span> (</span><br><span class="line">	<span class="keyword">select</span> si3.stu_id, <span class="built_in">avg</span>(si3.score) avg_score <span class="keyword">from</span> score_info si3 <span class="keyword">group</span> <span class="keyword">by</span> si3.stu_id</span><br><span class="line">) t1 <span class="keyword">on</span> si.stu_id <span class="operator">=</span> t1.stu_id</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> t1.avg_score <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line">stu_id<span class="operator">|</span>stu_name<span class="operator">|</span>score<span class="operator">|</span>avg_score         <span class="operator">|</span></span><br><span class="line"><span class="comment">------+--------+-----+------------------+</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>  <span class="number">100</span><span class="operator">|</span>             <span class="number">86.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>   <span class="number">84</span><span class="operator">|</span>             <span class="number">86.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>   <span class="number">87</span><span class="operator">|</span>             <span class="number">86.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">002</span>   <span class="operator">|</span>胡歌      <span class="operator">|</span>   <span class="number">74</span><span class="operator">|</span>             <span class="number">86.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span>   <span class="number">59</span><span class="operator">|</span>              <span class="number">81.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span>   <span class="number">85</span><span class="operator">|</span>              <span class="number">81.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span>   <span class="number">89</span><span class="operator">|</span>              <span class="number">81.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">004</span>   <span class="operator">|</span>刘德华     <span class="operator">|</span>   <span class="number">93</span><span class="operator">|</span>              <span class="number">81.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span>   <span class="number">71</span><span class="operator">|</span>             <span class="number">81.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span>   <span class="number">89</span><span class="operator">|</span>             <span class="number">81.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span>   <span class="number">94</span><span class="operator">|</span>             <span class="number">81.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">016</span>   <span class="operator">|</span>周冬雨     <span class="operator">|</span>   <span class="number">71</span><span class="operator">|</span>             <span class="number">81.25</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span>   <span class="number">44</span><span class="operator">|</span>              <span class="number">75.4</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span>   <span class="number">85</span><span class="operator">|</span>              <span class="number">75.4</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span>   <span class="number">64</span><span class="operator">|</span>              <span class="number">75.4</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span>   <span class="number">85</span><span class="operator">|</span>              <span class="number">75.4</span><span class="operator">|</span></span><br><span class="line"><span class="number">005</span>   <span class="operator">|</span>唐国强     <span class="operator">|</span>   <span class="number">99</span><span class="operator">|</span>              <span class="number">75.4</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span>   <span class="number">79</span><span class="operator">|</span>              <span class="number">74.2</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span>   <span class="number">79</span><span class="operator">|</span>              <span class="number">74.2</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span>   <span class="number">60</span><span class="operator">|</span>              <span class="number">74.2</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span>   <span class="number">75</span><span class="operator">|</span>              <span class="number">74.2</span><span class="operator">|</span></span><br><span class="line"><span class="number">009</span>   <span class="operator">|</span>郭德纲     <span class="operator">|</span>   <span class="number">78</span><span class="operator">|</span>              <span class="number">74.2</span><span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span>   <span class="number">71</span><span class="operator">|</span> <span class="number">73.33333333333333</span><span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span>   <span class="number">90</span><span class="operator">|</span> <span class="number">73.33333333333333</span><span class="operator">|</span></span><br><span class="line"><span class="number">006</span>   <span class="operator">|</span>陈道明     <span class="operator">|</span>   <span class="number">59</span><span class="operator">|</span> <span class="number">73.33333333333333</span><span class="operator">|</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span>   <span class="number">54</span><span class="operator">|</span>              <span class="number">72.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span>   <span class="number">63</span><span class="operator">|</span>              <span class="number">72.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span>   <span class="number">94</span><span class="operator">|</span>              <span class="number">72.5</span><span class="operator">|</span></span><br><span class="line"><span class="number">001</span>   <span class="operator">|</span>彭于晏     <span class="operator">|</span>   <span class="number">79</span><span class="operator">|</span>              <span class="number">72.5</span><span class="operator">|</span></span><br><span class="line">...</span><br></pre></td></tr></table></figure>


<h1 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h1><h2 id="一、函数简介"><a href="#一、函数简介" class="headerlink" title="一、函数简介"></a>一、函数简介</h2><p>Hive 会将常用的逻辑封装成函数给用户进行使用，类似于 Java 中的函数。</p>
<p><strong>好处</strong>：避免用户反复写逻辑，可以直接拿来使用。</p>
<p><strong>重点</strong>：用户需要知道函数叫什么，能做什么。</p>
<p>Hive 提供了大量的内置函数，按照其特点可大致分为如下几类：单行函数、聚合函数、炸裂函数、窗口函数。</p>
<ol>
<li><strong>查询系统内置函数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> functions;</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查看内置函数用法</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">desc</span> <span class="keyword">function</span> upper;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>查看内置函数详细信息</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">desc</span> <span class="keyword">function</span> extended upper;</span><br></pre></td></tr></table></figure>

<h2 id="二、单行函数"><a href="#二、单行函数" class="headerlink" title="二、单行函数"></a>二、单行函数</h2><p>单行函数的特点是一进一出，即输入一行，输出一行。</p>
<p>单行函数按照功能可分为如下几类: 日期函数、字符串函数、集合函数、数学函数、流程控制函数等。</p>
<h3 id="算术运算函数"><a href="#算术运算函数" class="headerlink" title="算术运算函数"></a>算术运算函数</h3><table>
<thead>
<tr>
<th>运算符</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>A+B</td>
<td>A和B 相加</td>
</tr>
<tr>
<td>A-B</td>
<td>A减去B</td>
</tr>
<tr>
<td>A*B</td>
<td>A和B 相乘</td>
</tr>
<tr>
<td>A&#x2F;B</td>
<td>A除以B</td>
</tr>
<tr>
<td>A%B</td>
<td>A对B取余</td>
</tr>
<tr>
<td>A&amp;B</td>
<td>A和B按位取与</td>
</tr>
<tr>
<td>A|B</td>
<td>A和B按位取或</td>
</tr>
<tr>
<td>A^B</td>
<td>A和B按位取异或</td>
</tr>
<tr>
<td>~A</td>
<td>A按位取反</td>
</tr>
</tbody></table>
<p><strong>查询所有员工薪水加1结果</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sal <span class="operator">+</span> <span class="number">1</span> <span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>

<h3 id="数值函数"><a href="#数值函数" class="headerlink" title="数值函数"></a>数值函数</h3><ol>
<li><strong>round：四舍五入</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> round(<span class="number">3.3</span>); <span class="comment">--3</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>ceil：向上取整</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">ceil</span>(<span class="number">3.1</span>); <span class="comment">--3</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>floor：向下取整</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">floor</span>(<span class="number">4.8</span>); <span class="comment">--4</span></span><br></pre></td></tr></table></figure>

<h3 id="字符串函数"><a href="#字符串函数" class="headerlink" title="字符串函数"></a>字符串函数</h3><h4 id="substring：截取字符串"><a href="#substring：截取字符串" class="headerlink" title="substring：截取字符串"></a>substring：截取字符串</h4><ol>
<li><strong>获取第二个字符之后的所有字符</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">substring</span>(<span class="string">&#x27;abcde&#x27;</span>, <span class="number">2</span>); <span class="comment">--bcde</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>获取倒数第三个字符串及以后字符</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">substring</span>(<span class="string">&#x27;abcde&#x27;</span>, <span class="number">-3</span>); <span class="comment">--cde</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>从第3个字符开始，向后取2个字符</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">substring</span>(<span class="string">&#x27;abcde&#x27;</span>, <span class="number">3</span>, <span class="number">2</span>); <span class="comment">--cd</span></span><br></pre></td></tr></table></figure>

<h4 id="replace：替换"><a href="#replace：替换" class="headerlink" title="replace：替换"></a>replace：替换</h4><p>将字符a替换为A</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> replace(<span class="string">&#x27;abcde&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;A&#x27;</span>); <span class="comment">--Abcde</span></span><br></pre></td></tr></table></figure>

<h4 id="regexp-replace：正则替换"><a href="#regexp-replace：正则替换" class="headerlink" title="regexp_replace：正则替换"></a>regexp_replace：正则替换</h4><p>使用正则将数字替换为英文</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> regexp_replace(<span class="string">&#x27;100 - 200&#x27;</span>, <span class="string">&#x27;(\\d+)&#x27;</span>, <span class="string">&#x27;num&#x27;</span>); <span class="comment">--num - num</span></span><br></pre></td></tr></table></figure>

<h4 id="regexp：正则匹配"><a href="#regexp：正则匹配" class="headerlink" title="regexp：正则匹配"></a>regexp：正则匹配</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="string">&#x27;abcdeeeee&#x27;</span> regexp <span class="string">&#x27;abcde+&#x27;</span>; <span class="comment">--true</span></span><br><span class="line"><span class="keyword">select</span> <span class="string">&#x27;abbbbcdeeeee&#x27;</span> regexp <span class="string">&#x27;ab+cde+&#x27;</span>; <span class="comment">--true</span></span><br><span class="line"><span class="keyword">select</span> <span class="string">&#x27;abbbbcdeeeee&#x27;</span> regexp <span class="string">&#x27;abcde+&#x27;</span>; <span class="comment">--false</span></span><br></pre></td></tr></table></figure>

<h4 id="repeat：重复字符串"><a href="#repeat：重复字符串" class="headerlink" title="repeat：重复字符串"></a>repeat：重复字符串</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> repeat(<span class="string">&#x27;abc&#x27;</span>, <span class="number">3</span>); <span class="comment">--abcabcabc</span></span><br></pre></td></tr></table></figure>

<h4 id="split：字符串切割"><a href="#split：字符串切割" class="headerlink" title="split：字符串切割"></a>split：字符串切割</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> split(<span class="string">&#x27;a-b-c-d-e&#x27;</span>, <span class="string">&#x27;-&#x27;</span>); <span class="comment">--[&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;]</span></span><br></pre></td></tr></table></figure>

<h4 id="nvl：替换null值"><a href="#nvl：替换null值" class="headerlink" title="nvl：替换null值"></a>nvl：替换null值</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> nvl(<span class="keyword">null</span>, <span class="number">1</span>); <span class="comment">--1</span></span><br></pre></td></tr></table></figure>

<h4 id="concat：拼接字符串"><a href="#concat：拼接字符串" class="headerlink" title="concat：拼接字符串"></a>concat：拼接字符串</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> concat(<span class="string">&#x27;abc&#x27;</span>, <span class="string">&#x27;-&#x27;</span>, <span class="string">&#x27;123&#x27;</span>, <span class="string">&#x27;-&#x27;</span>, <span class="string">&#x27;@@@&#x27;</span>); <span class="comment">--abc-123-@@@</span></span><br></pre></td></tr></table></figure>

<h4 id="concat-ws：指定分隔符拼接字符串或字符串数组"><a href="#concat-ws：指定分隔符拼接字符串或字符串数组" class="headerlink" title="concat_ws：指定分隔符拼接字符串或字符串数组"></a>concat_ws：指定分隔符拼接字符串或字符串数组</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> concat_ws(<span class="string">&#x27;-&#x27;</span>, <span class="keyword">array</span>(<span class="string">&#x27;aaa&#x27;</span>, <span class="string">&#x27;bbb&#x27;</span>, <span class="string">&#x27;ccc&#x27;</span>)); <span class="comment">--aaa-bbb-ccc</span></span><br></pre></td></tr></table></figure>

<h4 id="get-json-object：解析json字符串"><a href="#get-json-object：解析json字符串" class="headerlink" title="get_json_object：解析json字符串"></a>get_json_object：解析json字符串</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> get_json_object(</span><br><span class="line">    <span class="string">&#x27;[&#123;&quot;id&quot;:&quot;1001&quot;, &quot;name&quot;:&quot;zhangsan&quot;, &quot;age&quot;:&quot;17&quot;&#125;,&#123;&quot;id&quot;:&quot;1002&quot;, &quot;name&quot;:&quot;lisi&quot;, &quot;age&quot;:&quot;22&quot;&#125;]&#x27;</span>, </span><br><span class="line">    <span class="string">&#x27;$.[0].name&#x27;</span></span><br><span class="line">); <span class="comment">--zhansan</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> get_json_object(</span><br><span class="line">    <span class="string">&#x27;[&#123;&quot;id&quot;:&quot;1001&quot;, &quot;name&quot;:&quot;zhangsan&quot;, &quot;age&quot;:&quot;17&quot;&#125;,&#123;&quot;id&quot;:&quot;1002&quot;, &quot;name&quot;:&quot;lisi&quot;, &quot;age&quot;:&quot;22&quot;&#125;]&#x27;</span>, </span><br><span class="line">    <span class="string">&#x27;$.[0]&#x27;</span></span><br><span class="line">); <span class="comment">--&#123;&quot;id&quot;:&quot;1001&quot;,&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:&quot;17&quot;&#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="日期函数"><a href="#日期函数" class="headerlink" title="日期函数"></a>日期函数</h3><h4 id="unix-timestamp：返回当前或指定时间戳"><a href="#unix-timestamp：返回当前或指定时间戳" class="headerlink" title="unix_timestamp：返回当前或指定时间戳"></a>unix_timestamp：返回当前或指定时间戳</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> unix_timestamp(<span class="string">&#x27;2023/06/06 08-08-08&#x27;</span>,<span class="string">&#x27;yyyy/MM/dd HH-mm-ss&#x27;</span>); </span><br><span class="line"><span class="keyword">select</span> unix_timestamp(<span class="string">&#x27;2023-06-06 08-08-08&#x27;</span>,<span class="string">&#x27;yyyy-MM-dd HH-mm-ss&#x27;</span>); <span class="comment">--1686038888</span></span><br></pre></td></tr></table></figure>

<h4 id="from-unixtime：转化unix时间戳到当前时间"><a href="#from-unixtime：转化unix时间戳到当前时间" class="headerlink" title="from_unixtime：转化unix时间戳到当前时间"></a>from_unixtime：转化unix时间戳到当前时间</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> from_unixtime(<span class="number">1686038888</span>); <span class="comment">--2023-06-06 08:08:08</span></span><br></pre></td></tr></table></figure>

<h4 id="current-date：当前日期"><a href="#current-date：当前日期" class="headerlink" title="current_date：当前日期"></a>current_date：当前日期</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">current_date</span>; <span class="comment">--2023-06-07</span></span><br></pre></td></tr></table></figure>

<h4 id="current-timestamp：当前日期时间"><a href="#current-timestamp：当前日期时间" class="headerlink" title="current_timestamp：当前日期时间"></a>current_timestamp：当前日期时间</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="built_in">current_timestamp</span>; <span class="comment">--2023-06-07 06:47:49.901</span></span><br></pre></td></tr></table></figure>

<h4 id="month：获取指定日期中的月"><a href="#month：获取指定日期中的月" class="headerlink" title="month：获取指定日期中的月"></a>month：获取指定日期中的月</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">month</span>(<span class="string">&#x27;2023-05-26 15:33:28&#x27;</span>); <span class="comment">--5</span></span><br></pre></td></tr></table></figure>

<h4 id="day：获取指定日期中的日"><a href="#day：获取指定日期中的日" class="headerlink" title="day：获取指定日期中的日"></a>day：获取指定日期中的日</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">day</span>(<span class="string">&#x27;2023-05-26 15:33:28&#x27;</span>); <span class="comment">--26</span></span><br></pre></td></tr></table></figure>

<h4 id="hour：获取指定日期中的小时"><a href="#hour：获取指定日期中的小时" class="headerlink" title="hour：获取指定日期中的小时"></a>hour：获取指定日期中的小时</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">hour</span>(<span class="string">&#x27;2023-05-26 15:33:28&#x27;</span>); <span class="comment">--15</span></span><br></pre></td></tr></table></figure>

<h4 id="datediff：获取日期相差天数"><a href="#datediff：获取日期相差天数" class="headerlink" title="datediff：获取日期相差天数"></a>datediff：获取日期相差天数</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> datediff(<span class="string">&#x27;2023-06-05&#x27;</span>, <span class="string">&#x27;2023-05-02&#x27;</span>); <span class="comment">--34</span></span><br></pre></td></tr></table></figure>

<h4 id="date-add：日期加天数"><a href="#date-add：日期加天数" class="headerlink" title="date_add：日期加天数"></a>date_add：日期加天数</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> date_add(<span class="string">&#x27;2023-06-05&#x27;</span>, <span class="number">2</span>); <span class="comment">--2023-06-07</span></span><br></pre></td></tr></table></figure>

<h4 id="date-sub：日期减天数"><a href="#date-sub：日期减天数" class="headerlink" title="date_sub：日期减天数"></a>date_sub：日期减天数</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> date_sub(<span class="string">&#x27;2023-06-05&#x27;</span>, <span class="number">2</span>); <span class="comment">--2023-06-03</span></span><br></pre></td></tr></table></figure>

<h4 id="date-format：将日期解析成指定格式字符串"><a href="#date-format：将日期解析成指定格式字符串" class="headerlink" title="date_format：将日期解析成指定格式字符串"></a>date_format：将日期解析成指定格式字符串</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> date_format(<span class="string">&#x27;2023-06-05&#x27;</span>, <span class="string">&#x27;yyyy年MM月dd日&#x27;</span>); <span class="comment">--2023年06月05日</span></span><br></pre></td></tr></table></figure>

<h3 id="流程控制函数"><a href="#流程控制函数" class="headerlink" title="流程控制函数"></a>流程控制函数</h3><h4 id="case-when：条件判断函数"><a href="#case-when：条件判断函数" class="headerlink" title="case when：条件判断函数"></a>case when：条件判断函数</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--使用方式一</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">case</span> <span class="keyword">when</span> <span class="number">1</span><span class="operator">=</span><span class="number">2</span> <span class="keyword">then</span> <span class="string">&#x27;tom&#x27;</span> <span class="keyword">when</span> <span class="number">2</span><span class="operator">=</span><span class="number">2</span> <span class="keyword">then</span> <span class="string">&#x27;mary&#x27;</span> <span class="keyword">else</span> <span class="string">&#x27;time&#x27;</span> <span class="keyword">end</span> <span class="keyword">from</span> tablename;</span><br><span class="line"></span><br><span class="line"><span class="comment">--使用方式二</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">case</span> <span class="number">100</span> <span class="keyword">when</span> <span class="number">50</span> <span class="keyword">then</span> <span class="string">&#x27;tom&#x27;</span> <span class="keyword">when</span> <span class="number">100</span> <span class="keyword">then</span> <span class="string">&#x27;mary&#x27;</span> <span class="keyword">else</span> <span class="string">&#x27;tim&#x27;</span> <span class="keyword">end</span> <span class="keyword">from</span> tablename;</span><br></pre></td></tr></table></figure>

<h4 id="if：条件判断"><a href="#if：条件判断" class="headerlink" title="if：条件判断"></a>if：条件判断</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> if(<span class="number">10</span> <span class="operator">&gt;</span> <span class="number">5</span>, <span class="string">&#x27;true&#x27;</span>, <span class="string">&#x27;false&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h3 id="集合函数"><a href="#集合函数" class="headerlink" title="集合函数"></a>集合函数</h3><h4 id="array：声明集合"><a href="#array：声明集合" class="headerlink" title="array：声明集合"></a>array：声明集合</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>); <span class="comment">--[1,2,3,4]</span></span><br></pre></td></tr></table></figure>

<h4 id="size：集合元素个数"><a href="#size：集合元素个数" class="headerlink" title="size：集合元素个数"></a>size：集合元素个数</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> size(<span class="keyword">array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)); <span class="comment">--4</span></span><br></pre></td></tr></table></figure>

<h4 id="array-contains：判断array中是否包含元素"><a href="#array-contains：判断array中是否包含元素" class="headerlink" title="array_contains：判断array中是否包含元素"></a>array_contains：判断array中是否包含元素</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span>  array_contains(<span class="keyword">array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>), <span class="number">3</span>); <span class="comment">--true</span></span><br></pre></td></tr></table></figure>

<h4 id="sort-array：将aray排序"><a href="#sort-array：将aray排序" class="headerlink" title="sort_array：将aray排序"></a>sort_array：将aray排序</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span>  sort_array(<span class="keyword">array</span>(<span class="number">4</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>)); <span class="comment">--[1,2,3,4]</span></span><br></pre></td></tr></table></figure>

<h4 id="map：创建map集合"><a href="#map：创建map集合" class="headerlink" title="map：创建map集合"></a>map：创建map集合</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> map(<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>); <span class="comment">--&#123;&quot;a&quot;:1,&quot;b&quot;:2&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="map-keys：返回map中的key"><a href="#map-keys：返回map中的key" class="headerlink" title="map_keys：返回map中的key"></a>map_keys：返回map中的key</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> map_keys(map(<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>)); <span class="comment">--[&quot;a&quot;,&quot;b&quot;]</span></span><br></pre></td></tr></table></figure>

<h4 id="map-values：返回map中的value"><a href="#map-values：返回map中的value" class="headerlink" title="map_values：返回map中的value"></a>map_values：返回map中的value</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> map_values(map(<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>)); <span class="comment">--[1, 2]</span></span><br></pre></td></tr></table></figure>

<h4 id="struct：声明结构体"><a href="#struct：声明结构体" class="headerlink" title="struct：声明结构体"></a>struct：声明结构体</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> struct(<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>); <span class="comment">--[&#123;&quot;col1&quot;:&quot;a&quot;,&quot;col2&quot;:&quot;b&quot;,&quot;col3&quot;:&quot;c&quot;&#125;]</span></span><br></pre></td></tr></table></figure>

<h4 id="named-struct：声明结构体"><a href="#named-struct：声明结构体" class="headerlink" title="named_struct：声明结构体"></a>named_struct：声明结构体</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span>  named_struct(<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>, <span class="number">3</span>); <span class="comment">--[&#123;&quot;a&quot;:&quot;1&quot;,&quot;b&quot;:2,&quot;c&quot;:&quot;3&quot;&#125;]</span></span><br></pre></td></tr></table></figure>

<h3 id="练习"><a href="#练习" class="headerlink" title="练习"></a>练习</h3><ol>
<li><strong>数据准备</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">create</span>  <span class="keyword">table</span>  employee(</span><br><span class="line">    name string COMMENT <span class="string">&#x27;姓名&#x27;</span>,</span><br><span class="line">    sex  string COMMENT <span class="string">&#x27;性别&#x27;</span>,</span><br><span class="line">    birthday string COMMENT <span class="string">&#x27;出生年月&#x27;</span>,</span><br><span class="line">    hiredate string COMMENT <span class="string">&#x27;入职日期&#x27;</span>,</span><br><span class="line">    job string COMMENT <span class="string">&#x27;岗位&#x27;</span>,</span><br><span class="line">    salary <span class="keyword">double</span> COMMENT <span class="string">&#x27;薪资&#x27;</span>,</span><br><span class="line">    bonus <span class="keyword">double</span> COMMENT <span class="string">&#x27;奖金&#x27;</span>,</span><br><span class="line">    friends <span class="keyword">array</span><span class="operator">&lt;</span>string<span class="operator">&gt;</span> COMMENT <span class="string">&#x27;朋友&#x27;</span>,</span><br><span class="line">    children map<span class="operator">&lt;</span>string,<span class="type">int</span><span class="operator">&gt;</span> COMMENT <span class="string">&#x27;孩子&#x27;</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee  </span><br><span class="line"><span class="keyword">values</span>  (<span class="string">&#x27;张无忌&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="string">&#x27;1980/02/12&#x27;</span>,<span class="string">&#x27;2022/08/09&#x27;</span>,<span class="string">&#x27;销售&#x27;</span>,<span class="number">3000</span>,<span class="number">12000</span>,<span class="keyword">array</span>(<span class="string">&#x27;阿朱&#x27;</span>,<span class="string">&#x27;小昭&#x27;</span>),map(<span class="string">&#x27;张小无&#x27;</span>,<span class="number">8</span>,<span class="string">&#x27;张小忌&#x27;</span>,<span class="number">9</span>)),</span><br><span class="line">        (<span class="string">&#x27;赵敏&#x27;</span>,<span class="string">&#x27;女&#x27;</span>,<span class="string">&#x27;1982/05/18&#x27;</span>,<span class="string">&#x27;2022/09/10&#x27;</span>,<span class="string">&#x27;行政&#x27;</span>,<span class="number">9000</span>,<span class="number">2000</span>,<span class="keyword">array</span>(<span class="string">&#x27;阿三&#x27;</span>,<span class="string">&#x27;阿四&#x27;</span>),map(<span class="string">&#x27;赵小敏&#x27;</span>,<span class="number">8</span>)),</span><br><span class="line">        (<span class="string">&#x27;宋青书&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="string">&#x27;1981/03/15&#x27;</span>,<span class="string">&#x27;2022/04/09&#x27;</span>,<span class="string">&#x27;研发&#x27;</span>,<span class="number">18000</span>,<span class="number">1000</span>,<span class="keyword">array</span>(<span class="string">&#x27;王五&#x27;</span>,<span class="string">&#x27;赵六&#x27;</span>),map(<span class="string">&#x27;宋小青&#x27;</span>,<span class="number">7</span>,<span class="string">&#x27;宋小书&#x27;</span>,<span class="number">5</span>)),</span><br><span class="line">        (<span class="string">&#x27;周芷若&#x27;</span>,<span class="string">&#x27;女&#x27;</span>,<span class="string">&#x27;1981/03/17&#x27;</span>,<span class="string">&#x27;2022/04/10&#x27;</span>,<span class="string">&#x27;研发&#x27;</span>,<span class="number">18000</span>,<span class="number">1000</span>,<span class="keyword">array</span>(<span class="string">&#x27;王五&#x27;</span>,<span class="string">&#x27;赵六&#x27;</span>),map(<span class="string">&#x27;宋小青&#x27;</span>,<span class="number">7</span>,<span class="string">&#x27;宋小书&#x27;</span>,<span class="number">5</span>)),</span><br><span class="line">        (<span class="string">&#x27;郭靖&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="string">&#x27;1985/03/11&#x27;</span>,<span class="string">&#x27;2022/07/19&#x27;</span>,<span class="string">&#x27;销售&#x27;</span>,<span class="number">2000</span>,<span class="number">13000</span>,<span class="keyword">array</span>(<span class="string">&#x27;南帝&#x27;</span>,<span class="string">&#x27;北丐&#x27;</span>),map(<span class="string">&#x27;郭芙&#x27;</span>,<span class="number">5</span>,<span class="string">&#x27;郭襄&#x27;</span>,<span class="number">4</span>)),</span><br><span class="line">        (<span class="string">&#x27;黄蓉&#x27;</span>,<span class="string">&#x27;女&#x27;</span>,<span class="string">&#x27;1982/12/13&#x27;</span>,<span class="string">&#x27;2022/06/11&#x27;</span>,<span class="string">&#x27;行政&#x27;</span>,<span class="number">12000</span>,<span class="keyword">null</span>,<span class="keyword">array</span>(<span class="string">&#x27;东邪&#x27;</span>,<span class="string">&#x27;西毒&#x27;</span>),map(<span class="string">&#x27;郭芙&#x27;</span>,<span class="number">5</span>,<span class="string">&#x27;郭襄&#x27;</span>,<span class="number">4</span>)),</span><br><span class="line">        (<span class="string">&#x27;杨过&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="string">&#x27;1988/01/30&#x27;</span>,<span class="string">&#x27;2022/08/13&#x27;</span>,<span class="string">&#x27;前台&#x27;</span>,<span class="number">5000</span>,<span class="keyword">null</span>,<span class="keyword">array</span>(<span class="string">&#x27;郭靖&#x27;</span>,<span class="string">&#x27;黄蓉&#x27;</span>),map(<span class="string">&#x27;杨小过&#x27;</span>,<span class="number">2</span>)),</span><br><span class="line">        (<span class="string">&#x27;小龙女&#x27;</span>,<span class="string">&#x27;女&#x27;</span>,<span class="string">&#x27;1985/02/12&#x27;</span>,<span class="string">&#x27;2022/09/24&#x27;</span>,<span class="string">&#x27;前台&#x27;</span>,<span class="number">6000</span>,<span class="keyword">null</span>,<span class="keyword">array</span>(<span class="string">&#x27;张三&#x27;</span>,<span class="string">&#x27;李四&#x27;</span>),map(<span class="string">&#x27;杨小过&#x27;</span>,<span class="number">2</span>))</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>统计每个月的入职人数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	<span class="keyword">month</span>(replace(e.hiredate , <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;-&#x27;</span>)) <span class="keyword">month</span>, </span><br><span class="line">	<span class="built_in">count</span>(<span class="operator">*</span>) cnt</span><br><span class="line"><span class="keyword">from</span> employee e </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> <span class="keyword">month</span>(replace(e.hiredate , <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;-&#x27;</span>));</span><br><span class="line"></span><br><span class="line">_c0<span class="operator">|</span>cnt<span class="operator">|</span></span><br><span class="line"><span class="comment">---+---+</span></span><br><span class="line">  <span class="number">4</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">  <span class="number">6</span><span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span></span><br><span class="line">  <span class="number">7</span><span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span></span><br><span class="line">  <span class="number">8</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">  <span class="number">9</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>查询每个人的年龄（年 + 月）</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	t1.name,</span><br><span class="line">	concat(if(t1.month <span class="operator">&gt;=</span> <span class="number">0</span>, t1.year, t1.year<span class="number">-1</span>), <span class="string">&#x27;年&#x27;</span>, if(t1.month <span class="operator">&gt;=</span> <span class="number">0</span>, t1.month, <span class="number">12</span> <span class="operator">+</span> t1.month), <span class="string">&#x27;月&#x27;</span>) age</span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> </span><br><span class="line">		e.name,</span><br><span class="line">		replace(e.birthday, <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;-&#x27;</span>),</span><br><span class="line">		<span class="keyword">year</span>(<span class="built_in">current_date</span>()) <span class="operator">-</span> <span class="keyword">year</span>(replace(e.birthday, <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;-&#x27;</span>)) <span class="keyword">year</span>,</span><br><span class="line">		<span class="keyword">month</span>(<span class="built_in">current_date</span>()) <span class="operator">-</span> <span class="keyword">month</span>(replace(e.birthday, <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;-&#x27;</span>)) <span class="keyword">month</span></span><br><span class="line">	<span class="keyword">from</span> employee e </span><br><span class="line">) t1;</span><br><span class="line"></span><br><span class="line">name<span class="operator">|</span>age  <span class="operator">|</span></span><br><span class="line"><span class="comment">----+-----+</span></span><br><span class="line">张无忌 <span class="operator">|</span><span class="number">43</span>年<span class="number">4</span>月<span class="operator">|</span></span><br><span class="line">赵敏  <span class="operator">|</span><span class="number">41</span>年<span class="number">1</span>月<span class="operator">|</span></span><br><span class="line">宋青书 <span class="operator">|</span><span class="number">42</span>年<span class="number">3</span>月<span class="operator">|</span></span><br><span class="line">周芷若 <span class="operator">|</span><span class="number">42</span>年<span class="number">3</span>月<span class="operator">|</span></span><br><span class="line">郭靖  <span class="operator">|</span><span class="number">38</span>年<span class="number">3</span>月<span class="operator">|</span></span><br><span class="line">黄蓉  <span class="operator">|</span><span class="number">40</span>年<span class="number">6</span>月<span class="operator">|</span></span><br><span class="line">杨过  <span class="operator">|</span><span class="number">35</span>年<span class="number">5</span>月<span class="operator">|</span></span><br><span class="line">小龙女 <span class="operator">|</span><span class="number">38</span>年<span class="number">4</span>月<span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>按照薪资，奖金的和进行倒序排序，如果奖金为null，置位0</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	e.name,</span><br><span class="line">	e.salary <span class="operator">+</span> if (e.bonus <span class="keyword">is</span> <span class="keyword">null</span>, <span class="number">0</span>, e.bonus) sal </span><br><span class="line"><span class="keyword">from</span> employee e </span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> sal <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line">name<span class="operator">|</span>sal    <span class="operator">|</span></span><br><span class="line"><span class="comment">----+-------+</span></span><br><span class="line">周芷若 <span class="operator">|</span><span class="number">19000.0</span><span class="operator">|</span></span><br><span class="line">宋青书 <span class="operator">|</span><span class="number">19000.0</span><span class="operator">|</span></span><br><span class="line">郭靖  <span class="operator">|</span><span class="number">15000.0</span><span class="operator">|</span></span><br><span class="line">张无忌 <span class="operator">|</span><span class="number">15000.0</span><span class="operator">|</span></span><br><span class="line">黄蓉  <span class="operator">|</span><span class="number">12000.0</span><span class="operator">|</span></span><br><span class="line">赵敏  <span class="operator">|</span><span class="number">11000.0</span><span class="operator">|</span></span><br><span class="line">小龙女 <span class="operator">|</span> <span class="number">6000.0</span><span class="operator">|</span></span><br><span class="line">杨过  <span class="operator">|</span> <span class="number">5000.0</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="5">
<li><strong>查询每个人有多少个朋友</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	e.name,</span><br><span class="line">	size(e.friends) cnt</span><br><span class="line"><span class="keyword">from</span> employee e ;</span><br><span class="line"></span><br><span class="line">name<span class="operator">|</span>cnt<span class="operator">|</span></span><br><span class="line"><span class="comment">----+---+</span></span><br><span class="line">张无忌 <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">赵敏  <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">宋青书 <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">周芷若 <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">郭靖  <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">黄蓉  <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">杨过  <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">小龙女 <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="6">
<li><strong>查询每个人的孩子的姓名</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	e.name,</span><br><span class="line">	map_keys(e.children) ch_name</span><br><span class="line"><span class="keyword">from</span> employee e ;</span><br><span class="line"></span><br><span class="line">name<span class="operator">|</span>ch_name      <span class="operator">|</span></span><br><span class="line"><span class="comment">----+-------------+</span></span><br><span class="line">张无忌 <span class="operator">|</span>[&quot;张小无&quot;,&quot;张小忌&quot;]<span class="operator">|</span></span><br><span class="line">赵敏  <span class="operator">|</span>[&quot;赵小敏&quot;]      <span class="operator">|</span></span><br><span class="line">宋青书 <span class="operator">|</span>[&quot;宋小青&quot;,&quot;宋小书&quot;]<span class="operator">|</span></span><br><span class="line">周芷若 <span class="operator">|</span>[&quot;宋小青&quot;,&quot;宋小书&quot;]<span class="operator">|</span></span><br><span class="line">郭靖  <span class="operator">|</span>[&quot;郭芙&quot;,&quot;郭襄&quot;]  <span class="operator">|</span></span><br><span class="line">黄蓉  <span class="operator">|</span>[&quot;郭芙&quot;,&quot;郭襄&quot;]  <span class="operator">|</span></span><br><span class="line">杨过  <span class="operator">|</span>[&quot;杨小过&quot;]      <span class="operator">|</span></span><br><span class="line">小龙女 <span class="operator">|</span>[&quot;杨小过&quot;]      <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h2 id="三、高级聚合函数"><a href="#三、高级聚合函数" class="headerlink" title="三、高级聚合函数"></a>三、高级聚合函数</h2><p>collect_set()&#x2F;collect_list()：将字段进行汇总（set 去重，list 不去重），产生 Array 类型的字段</p>
<ol>
<li><strong>每个月入职人数以及姓名</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	<span class="keyword">month</span>(replace(e.hiredate, <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;-&#x27;</span>)) <span class="keyword">month</span>,</span><br><span class="line">	<span class="built_in">count</span>(<span class="operator">*</span>) cnt,</span><br><span class="line">	collect_list(e.name) name_list</span><br><span class="line"><span class="keyword">from</span> employee e </span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> <span class="keyword">month</span>(replace(e.hiredate, <span class="string">&#x27;/&#x27;</span>, <span class="string">&#x27;-&#x27;</span>));</span><br><span class="line"></span><br><span class="line"><span class="keyword">month</span><span class="operator">|</span>cnt<span class="operator">|</span>name_list    <span class="operator">|</span></span><br><span class="line"><span class="comment">-----+---+-------------+</span></span><br><span class="line">    <span class="number">4</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span>[&quot;宋青书&quot;,&quot;周芷若&quot;]<span class="operator">|</span></span><br><span class="line">    <span class="number">6</span><span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span>[&quot;黄蓉&quot;]       <span class="operator">|</span></span><br><span class="line">    <span class="number">7</span><span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span>[&quot;郭靖&quot;]       <span class="operator">|</span></span><br><span class="line">    <span class="number">8</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span>[&quot;张无忌&quot;,&quot;杨过&quot;] <span class="operator">|</span></span><br><span class="line">    <span class="number">9</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span>[&quot;赵敏&quot;,&quot;小龙女&quot;] <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h2 id="四、炸裂函数"><a href="#四、炸裂函数" class="headerlink" title="四、炸裂函数"></a>四、炸裂函数</h2><p>UDTF，接收一行数据，输出一行或多行数据</p>
<ol>
<li><strong>数据准备</strong></li>
</ol>
<table>
<thead>
<tr>
<th>movie</th>
<th>category</th>
</tr>
</thead>
<tbody><tr>
<td>《疑犯追踪》</td>
<td>悬疑，动作，科幻，剧情</td>
</tr>
<tr>
<td>《Lie to me》</td>
<td>悬疑，警匪，动作，心理，剧情</td>
</tr>
<tr>
<td>《战狼2》</td>
<td>战争，动作，灾难</td>
</tr>
</tbody></table>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> movie_info(</span><br><span class="line">    movie string COMMENT <span class="string">&#x27;电影名称&#x27;</span>,</span><br><span class="line">    category string COMMENT <span class="string">&#x27;电影分类&#x27;</span></span><br><span class="line">) </span><br><span class="line"><span class="type">row</span> format delimited fields terminated <span class="keyword">by</span> &quot;\t&quot;;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> movie_info</span><br><span class="line"><span class="keyword">values</span> (&quot;《疑犯追踪》&quot;, &quot;悬疑,动作,科幻,剧情&quot;),</span><br><span class="line">       (&quot;《Lie to me》&quot;, &quot;悬疑,警匪,动作,心理,剧情&quot;),</span><br><span class="line">       (&quot;《战狼2》&quot;, &quot;战争,动作,灾难&quot;);</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>根据表信息，统计各分类的电影数量，期望结果如下</strong></li>
</ol>
<table>
<thead>
<tr>
<th>cate</th>
<th>cnt</th>
</tr>
</thead>
<tbody><tr>
<td>剧情</td>
<td>2</td>
</tr>
<tr>
<td>动作</td>
<td>3</td>
</tr>
<tr>
<td>心理</td>
<td>1</td>
</tr>
<tr>
<td>悬疑</td>
<td>2</td>
</tr>
<tr>
<td>战争</td>
<td>1</td>
</tr>
<tr>
<td>灾难</td>
<td>1</td>
</tr>
<tr>
<td>科幻</td>
<td>1</td>
</tr>
<tr>
<td>警匪</td>
<td>1</td>
</tr>
</tbody></table>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--将category字段切分为集合 ==》 t1</span></span><br><span class="line"><span class="keyword">select</span> </span><br><span class="line">	mi.movie,</span><br><span class="line">	split(mi.category, <span class="string">&#x27;,&#x27;</span>) cates</span><br><span class="line"><span class="keyword">from</span> movie_info mi</span><br><span class="line"><span class="comment">--结果</span></span><br><span class="line">movie      <span class="operator">|</span>cates                     <span class="operator">|</span></span><br><span class="line"><span class="comment">-----------+--------------------------+</span></span><br><span class="line">《疑犯追踪》     <span class="operator">|</span>[&quot;悬疑&quot;,&quot;动作&quot;,&quot;科幻&quot;,&quot;剧情&quot;]     <span class="operator">|</span></span><br><span class="line">《Lie <span class="keyword">to</span> me》<span class="operator">|</span>[&quot;悬疑&quot;,&quot;警匪&quot;,&quot;动作&quot;,&quot;心理&quot;,&quot;剧情&quot;]<span class="operator">|</span></span><br><span class="line">《战狼<span class="number">2</span>》      <span class="operator">|</span>[&quot;战争&quot;,&quot;动作&quot;,&quot;灾难&quot;]          <span class="operator">|</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">--将cates集合字段炸开（将集合炸成多行） ==》 t2</span></span><br><span class="line"><span class="keyword">select</span> t1.movie, cate</span><br><span class="line"><span class="keyword">from</span> () t1 <span class="keyword">lateral</span> <span class="keyword">view</span> explode(t1.cates) tmp <span class="keyword">as</span> cate</span><br><span class="line"><span class="comment">--结果</span></span><br><span class="line">movie      <span class="operator">|</span>cate<span class="operator">|</span></span><br><span class="line"><span class="comment">-----------+----+</span></span><br><span class="line">《疑犯追踪》     <span class="operator">|</span>悬疑  <span class="operator">|</span></span><br><span class="line">《疑犯追踪》     <span class="operator">|</span>动作  <span class="operator">|</span></span><br><span class="line">《疑犯追踪》     <span class="operator">|</span>科幻  <span class="operator">|</span></span><br><span class="line">《疑犯追踪》     <span class="operator">|</span>剧情  <span class="operator">|</span></span><br><span class="line">《Lie <span class="keyword">to</span> me》<span class="operator">|</span>悬疑  <span class="operator">|</span></span><br><span class="line">《Lie <span class="keyword">to</span> me》<span class="operator">|</span>警匪  <span class="operator">|</span></span><br><span class="line">《Lie <span class="keyword">to</span> me》<span class="operator">|</span>动作  <span class="operator">|</span></span><br><span class="line">《Lie <span class="keyword">to</span> me》<span class="operator">|</span>心理  <span class="operator">|</span></span><br><span class="line">《Lie <span class="keyword">to</span> me》<span class="operator">|</span>剧情  <span class="operator">|</span></span><br><span class="line">《战狼<span class="number">2</span>》      <span class="operator">|</span>战争  <span class="operator">|</span></span><br><span class="line">《战狼<span class="number">2</span>》      <span class="operator">|</span>动作  <span class="operator">|</span></span><br><span class="line">《战狼<span class="number">2</span>》      <span class="operator">|</span>灾难  <span class="operator">|</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">--按照分类聚合统计</span></span><br><span class="line"><span class="keyword">select</span> t2.cate, <span class="built_in">count</span>(<span class="operator">*</span>) cnt</span><br><span class="line"><span class="keyword">from</span> ()t2</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> t2.cate;</span><br><span class="line"><span class="comment">--结果</span></span><br><span class="line">cate<span class="operator">|</span>cnt<span class="operator">|</span></span><br><span class="line"><span class="comment">----+---+</span></span><br><span class="line">剧情  <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">动作  <span class="operator">|</span>  <span class="number">3</span><span class="operator">|</span></span><br><span class="line">心理  <span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span></span><br><span class="line">悬疑  <span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span></span><br><span class="line">战争  <span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span></span><br><span class="line">灾难  <span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span></span><br><span class="line">科幻  <span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span></span><br><span class="line">警匪  <span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h2 id="五、窗口函数（开窗函数）"><a href="#五、窗口函数（开窗函数）" class="headerlink" title="五、窗口函数（开窗函数）"></a>五、窗口函数（开窗函数）</h2><p>窗口函数（window functions）：窗口函数，能为每行数据划分一个窗口，然后对窗口范围内的数据进行计算，最后将计算结果返回给该行数据</p>
<h3 id="常用窗口函数"><a href="#常用窗口函数" class="headerlink" title="常用窗口函数"></a>常用窗口函数</h3><ol>
<li><strong>聚合函数</strong></li>
</ol>
<p>max 最大值，min 最小值，sum 求和，avg 平均值，count 计数</p>
<ol start="2">
<li><strong>跨行取值函数</strong></li>
</ol>
<p>lead 和 lag：获取当前行的上&#x2F;下边某行或某个字段的值</p>
<p>first_value 和 last_value：获取窗口内某一列的第一个&#x2F;最后一个值</p>
<ol start="3">
<li><strong>排名函数</strong></li>
</ol>
<p>rank&#x2F;dense_rand&#x2F;row_number：计算排名</p>
<h3 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h3><ol>
<li><strong>数据准备</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> order_info</span><br><span class="line">(</span><br><span class="line">    order_id     string, <span class="comment">--订单id</span></span><br><span class="line">    user_id      string, <span class="comment">-- 用户id</span></span><br><span class="line">    user_name    string, <span class="comment">-- 用户姓名</span></span><br><span class="line">    order_date   string, <span class="comment">-- 下单日期</span></span><br><span class="line">    order_amount <span class="type">int</span>     <span class="comment">-- 订单金额</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> order_info</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;1001&#x27;</span>, <span class="string">&#x27;小元&#x27;</span>, <span class="string">&#x27;2022-01-01&#x27;</span>, <span class="string">&#x27;10&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;1002&#x27;</span>, <span class="string">&#x27;小海&#x27;</span>, <span class="string">&#x27;2022-01-02&#x27;</span>, <span class="string">&#x27;15&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;1001&#x27;</span>, <span class="string">&#x27;小元&#x27;</span>, <span class="string">&#x27;2022-02-03&#x27;</span>, <span class="string">&#x27;23&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;1002&#x27;</span>, <span class="string">&#x27;小海&#x27;</span>, <span class="string">&#x27;2022-01-04&#x27;</span>, <span class="string">&#x27;29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;1001&#x27;</span>, <span class="string">&#x27;小元&#x27;</span>, <span class="string">&#x27;2022-01-05&#x27;</span>, <span class="string">&#x27;46&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;1001&#x27;</span>, <span class="string">&#x27;小元&#x27;</span>, <span class="string">&#x27;2022-04-06&#x27;</span>, <span class="string">&#x27;42&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;1002&#x27;</span>, <span class="string">&#x27;小海&#x27;</span>, <span class="string">&#x27;2022-01-07&#x27;</span>, <span class="string">&#x27;50&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;1001&#x27;</span>, <span class="string">&#x27;小元&#x27;</span>, <span class="string">&#x27;2022-01-08&#x27;</span>, <span class="string">&#x27;50&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;1003&#x27;</span>, <span class="string">&#x27;小辉&#x27;</span>, <span class="string">&#x27;2022-04-08&#x27;</span>, <span class="string">&#x27;62&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;1003&#x27;</span>, <span class="string">&#x27;小辉&#x27;</span>, <span class="string">&#x27;2022-04-09&#x27;</span>, <span class="string">&#x27;62&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;1004&#x27;</span>, <span class="string">&#x27;小猛&#x27;</span>, <span class="string">&#x27;2022-05-10&#x27;</span>, <span class="string">&#x27;12&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;1003&#x27;</span>, <span class="string">&#x27;小辉&#x27;</span>, <span class="string">&#x27;2022-04-11&#x27;</span>, <span class="string">&#x27;75&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;13&#x27;</span>, <span class="string">&#x27;1004&#x27;</span>, <span class="string">&#x27;小猛&#x27;</span>, <span class="string">&#x27;2022-06-12&#x27;</span>, <span class="string">&#x27;80&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;14&#x27;</span>, <span class="string">&#x27;1003&#x27;</span>, <span class="string">&#x27;小辉&#x27;</span>, <span class="string">&#x27;2022-04-13&#x27;</span>, <span class="string">&#x27;94&#x27;</span>);</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>统计每个用户截至每次下单的累积下单总额</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	oi.order_id,</span><br><span class="line">	oi.user_id,</span><br><span class="line">	oi.user_name,</span><br><span class="line">	oi.order_date,</span><br><span class="line">	<span class="built_in">sum</span>(oi.order_amount) <span class="keyword">over</span> (<span class="keyword">partition</span> <span class="keyword">by</span> oi.user_id <span class="keyword">order</span> <span class="keyword">by</span> oi.order_date <span class="keyword">rows</span> <span class="keyword">between</span> unbounded preceding <span class="keyword">and</span> <span class="keyword">current</span> <span class="type">row</span>) sum_so_far</span><br><span class="line"><span class="keyword">from</span> order_info oi ;</span><br><span class="line"></span><br><span class="line">order_id<span class="operator">|</span>user_id<span class="operator">|</span>user_name<span class="operator">|</span>order_date<span class="operator">|</span>sum_so_far<span class="operator">|</span></span><br><span class="line"><span class="comment">--------+-------+---------+----------+----------+</span></span><br><span class="line"><span class="number">1</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-01</span><span class="operator">|</span>        <span class="number">10</span><span class="operator">|</span></span><br><span class="line"><span class="number">5</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-05</span><span class="operator">|</span>        <span class="number">56</span><span class="operator">|</span></span><br><span class="line"><span class="number">8</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-08</span><span class="operator">|</span>       <span class="number">106</span><span class="operator">|</span></span><br><span class="line"><span class="number">3</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-02</span><span class="number">-03</span><span class="operator">|</span>       <span class="number">129</span><span class="operator">|</span></span><br><span class="line"><span class="number">6</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-06</span><span class="operator">|</span>       <span class="number">171</span><span class="operator">|</span></span><br><span class="line"><span class="number">2</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-02</span><span class="operator">|</span>        <span class="number">15</span><span class="operator">|</span></span><br><span class="line"><span class="number">4</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-04</span><span class="operator">|</span>        <span class="number">44</span><span class="operator">|</span></span><br><span class="line"><span class="number">7</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-07</span><span class="operator">|</span>        <span class="number">94</span><span class="operator">|</span></span><br><span class="line"><span class="number">9</span>       <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-08</span><span class="operator">|</span>        <span class="number">62</span><span class="operator">|</span></span><br><span class="line"><span class="number">10</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-09</span><span class="operator">|</span>       <span class="number">124</span><span class="operator">|</span></span><br><span class="line"><span class="number">12</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-11</span><span class="operator">|</span>       <span class="number">199</span><span class="operator">|</span></span><br><span class="line"><span class="number">14</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-13</span><span class="operator">|</span>       <span class="number">293</span><span class="operator">|</span></span><br><span class="line"><span class="number">11</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-05</span><span class="number">-10</span><span class="operator">|</span>        <span class="number">12</span><span class="operator">|</span></span><br><span class="line"><span class="number">13</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-06</span><span class="number">-12</span><span class="operator">|</span>        <span class="number">92</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>统计每个用户截至每次下单的，当月累积下单总额</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	oi.order_id,</span><br><span class="line">	oi.user_id,</span><br><span class="line">	oi.user_name,</span><br><span class="line">	oi.order_date,</span><br><span class="line">	<span class="built_in">sum</span>(oi.order_amount) <span class="keyword">over</span> (<span class="keyword">partition</span> <span class="keyword">by</span> oi.user_id, <span class="built_in">substring</span>(oi.order_date, <span class="number">1</span>, <span class="number">7</span>) <span class="keyword">order</span> <span class="keyword">by</span> oi.order_date <span class="keyword">rows</span> <span class="keyword">between</span> unbounded preceding <span class="keyword">and</span> <span class="keyword">current</span> <span class="type">row</span>) sum_so_far</span><br><span class="line"><span class="keyword">from</span> order_info oi ;</span><br><span class="line"></span><br><span class="line">order_id<span class="operator">|</span>user_id<span class="operator">|</span>user_name<span class="operator">|</span>order_date<span class="operator">|</span>sum_so_far<span class="operator">|</span></span><br><span class="line"><span class="comment">--------+-------+---------+----------+----------+</span></span><br><span class="line"><span class="number">1</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-01</span><span class="operator">|</span>        <span class="number">10</span><span class="operator">|</span></span><br><span class="line"><span class="number">5</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-05</span><span class="operator">|</span>        <span class="number">56</span><span class="operator">|</span></span><br><span class="line"><span class="number">8</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-08</span><span class="operator">|</span>       <span class="number">106</span><span class="operator">|</span></span><br><span class="line"><span class="number">3</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-02</span><span class="number">-03</span><span class="operator">|</span>        <span class="number">23</span><span class="operator">|</span></span><br><span class="line"><span class="number">6</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-06</span><span class="operator">|</span>        <span class="number">42</span><span class="operator">|</span></span><br><span class="line"><span class="number">2</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-02</span><span class="operator">|</span>        <span class="number">15</span><span class="operator">|</span></span><br><span class="line"><span class="number">4</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-04</span><span class="operator">|</span>        <span class="number">44</span><span class="operator">|</span></span><br><span class="line"><span class="number">7</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-07</span><span class="operator">|</span>        <span class="number">94</span><span class="operator">|</span></span><br><span class="line"><span class="number">9</span>       <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-08</span><span class="operator">|</span>        <span class="number">62</span><span class="operator">|</span></span><br><span class="line"><span class="number">10</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-09</span><span class="operator">|</span>       <span class="number">124</span><span class="operator">|</span></span><br><span class="line"><span class="number">12</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-11</span><span class="operator">|</span>       <span class="number">199</span><span class="operator">|</span></span><br><span class="line"><span class="number">14</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-13</span><span class="operator">|</span>       <span class="number">293</span><span class="operator">|</span></span><br><span class="line"><span class="number">11</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-05</span><span class="number">-10</span><span class="operator">|</span>        <span class="number">12</span><span class="operator">|</span></span><br><span class="line"><span class="number">13</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-06</span><span class="number">-12</span><span class="operator">|</span>        <span class="number">80</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>统计每个用户每次下单距离上次下单相隔的天数（首次下单按0天算）</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span></span><br><span class="line">    order_id,</span><br><span class="line">    user_id,</span><br><span class="line">    user_name,</span><br><span class="line">    order_date,</span><br><span class="line">    order_amount,</span><br><span class="line">    nvl(datediff(order_date,last_order_date),<span class="number">0</span>) diff</span><br><span class="line"><span class="keyword">from</span></span><br><span class="line">(</span><br><span class="line">    <span class="keyword">select</span></span><br><span class="line">        order_id,</span><br><span class="line">        user_id,</span><br><span class="line">        user_name,</span><br><span class="line">        order_date,</span><br><span class="line">        order_amount,</span><br><span class="line">        <span class="built_in">lag</span>(order_date,<span class="number">1</span>,<span class="keyword">null</span>) <span class="keyword">over</span>(<span class="keyword">partition</span> <span class="keyword">by</span> user_id <span class="keyword">order</span> <span class="keyword">by</span> order_date) last_order_date</span><br><span class="line">    <span class="keyword">from</span> order_info</span><br><span class="line">)t1;</span><br><span class="line"></span><br><span class="line">order_id<span class="operator">|</span>user_id<span class="operator">|</span>user_name<span class="operator">|</span>order_date<span class="operator">|</span>order_amount<span class="operator">|</span>diff<span class="operator">|</span></span><br><span class="line"><span class="comment">--------+-------+---------+----------+------------+----+</span></span><br><span class="line"><span class="number">1</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-01</span><span class="operator">|</span>          <span class="number">10</span><span class="operator">|</span>   <span class="number">0</span><span class="operator">|</span></span><br><span class="line"><span class="number">5</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-05</span><span class="operator">|</span>          <span class="number">46</span><span class="operator">|</span>   <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">8</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-08</span><span class="operator">|</span>          <span class="number">50</span><span class="operator">|</span>   <span class="number">3</span><span class="operator">|</span></span><br><span class="line"><span class="number">3</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-02</span><span class="number">-03</span><span class="operator">|</span>          <span class="number">23</span><span class="operator">|</span>  <span class="number">26</span><span class="operator">|</span></span><br><span class="line"><span class="number">6</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-06</span><span class="operator">|</span>          <span class="number">42</span><span class="operator">|</span>  <span class="number">62</span><span class="operator">|</span></span><br><span class="line"><span class="number">2</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-02</span><span class="operator">|</span>          <span class="number">15</span><span class="operator">|</span>   <span class="number">0</span><span class="operator">|</span></span><br><span class="line"><span class="number">4</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-04</span><span class="operator">|</span>          <span class="number">29</span><span class="operator">|</span>   <span class="number">2</span><span class="operator">|</span></span><br><span class="line"><span class="number">7</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-07</span><span class="operator">|</span>          <span class="number">50</span><span class="operator">|</span>   <span class="number">3</span><span class="operator">|</span></span><br><span class="line"><span class="number">9</span>       <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-08</span><span class="operator">|</span>          <span class="number">62</span><span class="operator">|</span>   <span class="number">0</span><span class="operator">|</span></span><br><span class="line"><span class="number">10</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-09</span><span class="operator">|</span>          <span class="number">62</span><span class="operator">|</span>   <span class="number">1</span><span class="operator">|</span></span><br><span class="line"><span class="number">12</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-11</span><span class="operator">|</span>          <span class="number">75</span><span class="operator">|</span>   <span class="number">2</span><span class="operator">|</span></span><br><span class="line"><span class="number">14</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-13</span><span class="operator">|</span>          <span class="number">94</span><span class="operator">|</span>   <span class="number">2</span><span class="operator">|</span></span><br><span class="line"><span class="number">11</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-05</span><span class="number">-10</span><span class="operator">|</span>          <span class="number">12</span><span class="operator">|</span>   <span class="number">0</span><span class="operator">|</span></span><br><span class="line"><span class="number">13</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-06</span><span class="number">-12</span><span class="operator">|</span>          <span class="number">80</span><span class="operator">|</span>  <span class="number">33</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="5">
<li><strong>查询所有下单记录以及每个用户的每个下单记录所在月份的首&#x2F;末次下单日期</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span></span><br><span class="line">    order_id,</span><br><span class="line">    user_id,</span><br><span class="line">    user_name,</span><br><span class="line">    order_date,</span><br><span class="line">    order_amount,</span><br><span class="line">    <span class="built_in">first_value</span>(order_date) <span class="keyword">over</span>(<span class="keyword">partition</span> <span class="keyword">by</span> user_id,<span class="built_in">substring</span>(order_date,<span class="number">1</span>,<span class="number">7</span>) <span class="keyword">order</span> <span class="keyword">by</span> order_date) first_date,</span><br><span class="line">    <span class="built_in">last_value</span>(order_date) <span class="keyword">over</span>(<span class="keyword">partition</span> <span class="keyword">by</span> user_id,<span class="built_in">substring</span>(order_date,<span class="number">1</span>,<span class="number">7</span>) <span class="keyword">order</span> <span class="keyword">by</span> order_date <span class="keyword">rows</span> <span class="keyword">between</span> unbounded preceding <span class="keyword">and</span> unbounded following) last_date</span><br><span class="line"><span class="keyword">from</span> order_info;</span><br><span class="line"></span><br><span class="line">order_id<span class="operator">|</span>user_id<span class="operator">|</span>user_name<span class="operator">|</span>order_date<span class="operator">|</span>order_amount<span class="operator">|</span>first_date<span class="operator">|</span>last_date <span class="operator">|</span></span><br><span class="line"><span class="comment">--------+-------+---------+----------+------------+----------+----------+</span></span><br><span class="line"><span class="number">1</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-01</span><span class="operator">|</span>          <span class="number">10</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-01</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-08</span><span class="operator">|</span></span><br><span class="line"><span class="number">5</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-05</span><span class="operator">|</span>          <span class="number">46</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-01</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-08</span><span class="operator">|</span></span><br><span class="line"><span class="number">8</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-08</span><span class="operator">|</span>          <span class="number">50</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-01</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-08</span><span class="operator">|</span></span><br><span class="line"><span class="number">3</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-02</span><span class="number">-03</span><span class="operator">|</span>          <span class="number">23</span><span class="operator">|</span><span class="number">2022</span><span class="number">-02</span><span class="number">-03</span><span class="operator">|</span><span class="number">2022</span><span class="number">-02</span><span class="number">-03</span><span class="operator">|</span></span><br><span class="line"><span class="number">6</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-06</span><span class="operator">|</span>          <span class="number">42</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-06</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-06</span><span class="operator">|</span></span><br><span class="line"><span class="number">2</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-02</span><span class="operator">|</span>          <span class="number">15</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-02</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-07</span><span class="operator">|</span></span><br><span class="line"><span class="number">4</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-04</span><span class="operator">|</span>          <span class="number">29</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-02</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-07</span><span class="operator">|</span></span><br><span class="line"><span class="number">7</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-07</span><span class="operator">|</span>          <span class="number">50</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-02</span><span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-07</span><span class="operator">|</span></span><br><span class="line"><span class="number">9</span>       <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-08</span><span class="operator">|</span>          <span class="number">62</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-08</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-13</span><span class="operator">|</span></span><br><span class="line"><span class="number">10</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-09</span><span class="operator">|</span>          <span class="number">62</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-08</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-13</span><span class="operator">|</span></span><br><span class="line"><span class="number">12</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-11</span><span class="operator">|</span>          <span class="number">75</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-08</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-13</span><span class="operator">|</span></span><br><span class="line"><span class="number">14</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-13</span><span class="operator">|</span>          <span class="number">94</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-08</span><span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-13</span><span class="operator">|</span></span><br><span class="line"><span class="number">11</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-05</span><span class="number">-10</span><span class="operator">|</span>          <span class="number">12</span><span class="operator">|</span><span class="number">2022</span><span class="number">-05</span><span class="number">-10</span><span class="operator">|</span><span class="number">2022</span><span class="number">-05</span><span class="number">-10</span><span class="operator">|</span></span><br><span class="line"><span class="number">13</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-06</span><span class="number">-12</span><span class="operator">|</span>          <span class="number">80</span><span class="operator">|</span><span class="number">2022</span><span class="number">-06</span><span class="number">-12</span><span class="operator">|</span><span class="number">2022</span><span class="number">-06</span><span class="number">-12</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<ol start="6">
<li><strong>为每个用户的所有下单记录按照订单金额进行排名</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span></span><br><span class="line">    order_id,</span><br><span class="line">    user_id,</span><br><span class="line">    user_name,</span><br><span class="line">    order_date,</span><br><span class="line">    order_amount,</span><br><span class="line">    <span class="built_in">rank</span>() <span class="keyword">over</span>(<span class="keyword">partition</span> <span class="keyword">by</span> user_id <span class="keyword">order</span> <span class="keyword">by</span> order_amount <span class="keyword">desc</span>) rk,</span><br><span class="line">    <span class="built_in">dense_rank</span>() <span class="keyword">over</span>(<span class="keyword">partition</span> <span class="keyword">by</span> user_id <span class="keyword">order</span> <span class="keyword">by</span> order_amount <span class="keyword">desc</span>) drk,</span><br><span class="line">    <span class="built_in">row_number</span>() <span class="keyword">over</span>(<span class="keyword">partition</span> <span class="keyword">by</span> user_id <span class="keyword">order</span> <span class="keyword">by</span> order_amount <span class="keyword">desc</span>) rn</span><br><span class="line"><span class="keyword">from</span> order_info;</span><br><span class="line"></span><br><span class="line">order_id<span class="operator">|</span>user_id<span class="operator">|</span>user_name<span class="operator">|</span>order_date<span class="operator">|</span>order_amount<span class="operator">|</span>rk<span class="operator">|</span>drk<span class="operator">|</span>rn<span class="operator">|</span></span><br><span class="line"><span class="comment">--------+-------+---------+----------+------------+--+---+--+</span></span><br><span class="line"><span class="number">8</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-08</span><span class="operator">|</span>          <span class="number">50</span><span class="operator">|</span> <span class="number">1</span><span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span> <span class="number">1</span><span class="operator">|</span></span><br><span class="line"><span class="number">5</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-05</span><span class="operator">|</span>          <span class="number">46</span><span class="operator">|</span> <span class="number">2</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span> <span class="number">2</span><span class="operator">|</span></span><br><span class="line"><span class="number">6</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-06</span><span class="operator">|</span>          <span class="number">42</span><span class="operator">|</span> <span class="number">3</span><span class="operator">|</span>  <span class="number">3</span><span class="operator">|</span> <span class="number">3</span><span class="operator">|</span></span><br><span class="line"><span class="number">3</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-02</span><span class="number">-03</span><span class="operator">|</span>          <span class="number">23</span><span class="operator">|</span> <span class="number">4</span><span class="operator">|</span>  <span class="number">4</span><span class="operator">|</span> <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">1</span>       <span class="operator">|</span><span class="number">1001</span>   <span class="operator">|</span>小元       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-01</span><span class="operator">|</span>          <span class="number">10</span><span class="operator">|</span> <span class="number">5</span><span class="operator">|</span>  <span class="number">5</span><span class="operator">|</span> <span class="number">5</span><span class="operator">|</span></span><br><span class="line"><span class="number">7</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-07</span><span class="operator">|</span>          <span class="number">50</span><span class="operator">|</span> <span class="number">1</span><span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span> <span class="number">1</span><span class="operator">|</span></span><br><span class="line"><span class="number">4</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-04</span><span class="operator">|</span>          <span class="number">29</span><span class="operator">|</span> <span class="number">2</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span> <span class="number">2</span><span class="operator">|</span></span><br><span class="line"><span class="number">2</span>       <span class="operator">|</span><span class="number">1002</span>   <span class="operator">|</span>小海       <span class="operator">|</span><span class="number">2022</span><span class="number">-01</span><span class="number">-02</span><span class="operator">|</span>          <span class="number">15</span><span class="operator">|</span> <span class="number">3</span><span class="operator">|</span>  <span class="number">3</span><span class="operator">|</span> <span class="number">3</span><span class="operator">|</span></span><br><span class="line"><span class="number">14</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-13</span><span class="operator">|</span>          <span class="number">94</span><span class="operator">|</span> <span class="number">1</span><span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span> <span class="number">1</span><span class="operator">|</span></span><br><span class="line"><span class="number">12</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-11</span><span class="operator">|</span>          <span class="number">75</span><span class="operator">|</span> <span class="number">2</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span> <span class="number">2</span><span class="operator">|</span></span><br><span class="line"><span class="number">9</span>       <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-08</span><span class="operator">|</span>          <span class="number">62</span><span class="operator">|</span> <span class="number">3</span><span class="operator">|</span>  <span class="number">3</span><span class="operator">|</span> <span class="number">3</span><span class="operator">|</span></span><br><span class="line"><span class="number">10</span>      <span class="operator">|</span><span class="number">1003</span>   <span class="operator">|</span>小辉       <span class="operator">|</span><span class="number">2022</span><span class="number">-04</span><span class="number">-09</span><span class="operator">|</span>          <span class="number">62</span><span class="operator">|</span> <span class="number">3</span><span class="operator">|</span>  <span class="number">3</span><span class="operator">|</span> <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">13</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-06</span><span class="number">-12</span><span class="operator">|</span>          <span class="number">80</span><span class="operator">|</span> <span class="number">1</span><span class="operator">|</span>  <span class="number">1</span><span class="operator">|</span> <span class="number">1</span><span class="operator">|</span></span><br><span class="line"><span class="number">11</span>      <span class="operator">|</span><span class="number">1004</span>   <span class="operator">|</span>小猛       <span class="operator">|</span><span class="number">2022</span><span class="number">-05</span><span class="number">-10</span><span class="operator">|</span>          <span class="number">12</span><span class="operator">|</span> <span class="number">2</span><span class="operator">|</span>  <span class="number">2</span><span class="operator">|</span> <span class="number">2</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h2 id="六、自定义函数"><a href="#六、自定义函数" class="headerlink" title="六、自定义函数"></a>六、自定义函数</h2><p>Hive自带了一些函数，比如：max&#x2F;min等，但是数量有限，自己可以通过自定义UDF来方便的扩展。</p>
<p>当Hive提供的内置函数无法满足你的业务处理需要时，此时就可以考虑使用用户自定义函数（UDF：user-defined function）。</p>
<p>根据用户自定义函数类别分为以下三种：</p>
<ul>
<li><p>UDF（User-Defined-Function）</p>
<ul>
<li>一进一出。</li>
</ul>
</li>
<li><p>UDAF（User-Defined Aggregation Function）</p>
<ul>
<li>用户自定义聚合函数，多进一出。</li>
<li>类似于：count&#x2F;max&#x2F;min</li>
</ul>
</li>
<li><p>UDTF（User-Defined Table-Generating Functions）</p>
</li>
<li><p>用户自定义表生成函数，一进多出。</p>
</li>
<li><p>如lateral view explode()</p>
</li>
</ul>
<p>官方文档地址：<a target="_blank" rel="noopener" href="https://cwiki.apache.org/confluence/display/Hive/HivePlugins">https://cwiki.apache.org/confluence/display/Hive/HivePlugins</a></p>
<p>编程步骤</p>
<ul>
<li>继承 Hive 提供的类</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">org.apache.hadoop.hive.ql.udf.generic.GenericUDF</span><br><span class="line">org.apache.hadoop.hive.ql.udf.generic.GenericUDTF</span><br></pre></td></tr></table></figure>

<ul>
<li>实现类中的抽象方法</li>
<li>在 Hive 的命令行窗口，创建函数</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--添加jar</span></span><br><span class="line"><span class="keyword">add</span> jar linux_jar_path</span><br><span class="line"></span><br><span class="line"><span class="comment">--创建函数</span></span><br><span class="line"><span class="keyword">create</span> [temporary] <span class="keyword">function</span> [dbname.]function_name <span class="keyword">AS</span> class_name;</span><br></pre></td></tr></table></figure>

<ul>
<li>在 Hive 的命令行窗口，删除函数</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> [temporary] <span class="keyword">function</span> [if <span class="keyword">exists</span>] [dbname.]function_name;</span><br></pre></td></tr></table></figure>

<h2 id="七、自定义-UDF-函数"><a href="#七、自定义-UDF-函数" class="headerlink" title="七、自定义 UDF 函数"></a>七、自定义 UDF 函数</h2><h3 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h3><p>自定义 UDF 函数，实现给定基本数据类型的长度，如</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> my_len(<span class="string">&#x27;abcd&#x27;</span>); <span class="comment">--返回4</span></span><br></pre></td></tr></table></figure>

<h3 id="自定义-UDF-函数实现"><a href="#自定义-UDF-函数实现" class="headerlink" title="自定义 UDF 函数实现"></a>自定义 UDF 函数实现</h3><ol>
<li><strong>创建 maven 工程</strong></li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.example.hive<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>HiveDemo<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>导入依赖</strong></li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.hive<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>hive-exec<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.1.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>创建类继承 GenericUDF</strong></li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.example.hive.udf;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.hadoop.hive.ql.exec.UDFArgumentException;</span><br><span class="line"><span class="keyword">import</span> org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;</span><br><span class="line"><span class="keyword">import</span> org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;</span><br><span class="line"><span class="keyword">import</span> org.apache.hadoop.hive.ql.metadata.HiveException;</span><br><span class="line"><span class="keyword">import</span> org.apache.hadoop.hive.ql.udf.generic.GenericUDF;</span><br><span class="line"><span class="keyword">import</span> org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;</span><br><span class="line"><span class="keyword">import</span> org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span> TODO 判断传入的参数长度，交返回</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyUDF</span> <span class="keyword">extends</span> <span class="title class_">GenericUDF</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断参数，并约定返回数据类型</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> objectInspectors</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> UDFArgumentException</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> ObjectInspector <span class="title function_">initialize</span><span class="params">(ObjectInspector[] objectInspectors)</span> <span class="keyword">throws</span> UDFArgumentException &#123;</span><br><span class="line">        <span class="keyword">if</span> (objectInspectors.length != <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">UDFArgumentLengthException</span>(<span class="string">&quot;please input only one arg!&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!objectInspectors[<span class="number">0</span>].getCategory().equals(ObjectInspector.Category.PRIMITIVE)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">UDFArgumentTypeException</span>(<span class="number">1</span>, <span class="string">&quot;please input primitive type arg!&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> PrimitiveObjectInspectorFactory.javaIntObjectInspector;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 解决具体逻辑</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> deferredObjects</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> HiveException</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> Object <span class="title function_">evaluate</span><span class="params">(DeferredObject[] deferredObjects)</span> <span class="keyword">throws</span> HiveException &#123;</span><br><span class="line">        <span class="type">Object</span> <span class="variable">o</span> <span class="operator">=</span> deferredObjects[<span class="number">0</span>].get();</span><br><span class="line">        <span class="keyword">if</span> (o == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> o.toString().length();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取解释的字符串</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> strings</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getDisplayString</span><span class="params">(String[] strings)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>创建临时函数</strong></li>
</ol>
<p>打成 jar 包，上传服务器 &#x2F;opt&#x2F;module&#x2F;hive-3.1.3&#x2F;datas&#x2F;myudf.jar</p>
<p>将 jar 包添加到 Hive 的 classpath，临时生效</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">add</span> jar <span class="operator">/</span>opt<span class="operator">/</span><span class="keyword">module</span><span class="operator">/</span>hive<span class="number">-3.1</span><span class="number">.3</span><span class="operator">/</span>datas<span class="operator">/</span>myudf.jar;</span><br></pre></td></tr></table></figure>

<p>创建临时函数，并与 jar 关联</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> temporary <span class="keyword">function</span> my_len <span class="keyword">as</span> <span class="string">&#x27;com.example.hive.udf.MyUDF&#x27;</span>;</span><br></pre></td></tr></table></figure>

<ol start="5">
<li><strong>使用函数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> my_len(<span class="string">&#x27;abc&#x27;</span>);</span><br></pre></td></tr></table></figure>

<ol start="6">
<li><strong>删除临时函数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> temporary <span class="keyword">function</span> my_len;</span><br></pre></td></tr></table></figure>

<p>注意：临时函数只跟会话有关系，跟库没有关系。只要创建临时函数的会话不退出，在此会话下，任意一个库都可以使用，其他会话不可以。</p>
<ol start="7">
<li><strong>创建永久函数</strong></li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">function</span> my_len2 <span class="keyword">as</span> &quot;com.example.hive.udf.MyUDF&quot;</span><br><span class="line"><span class="keyword">using</span> jar &quot;hdfs://hadoop102:8020/udf/myudf.jar&quot;</span><br></pre></td></tr></table></figure>

<p>创建完成即可使用，删除 drop function my_len2;</p>
<h1 id="综合案例练习二"><a href="#综合案例练习二" class="headerlink" title="综合案例练习二"></a>综合案例练习二</h1><h3 id="环境准备"><a href="#环境准备" class="headerlink" title="环境准备"></a>环境准备</h3><h4 id="用户信息表"><a href="#用户信息表" class="headerlink" title="用户信息表"></a>用户信息表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> user_info;</span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> user_info;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> user_info(</span><br><span class="line">    `user_id`  string COMMENT <span class="string">&#x27;用户id&#x27;</span>,</span><br><span class="line">    `gender`   string COMMENT <span class="string">&#x27;性别&#x27;</span>,</span><br><span class="line">    `birthday` string COMMENT <span class="string">&#x27;生日&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;用户信息表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> user_info</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;1990-01-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="string">&#x27;1991-02-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="string">&#x27;1992-03-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;1993-04-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="string">&#x27;1994-05-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;1995-06-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="string">&#x27;1996-07-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;1997-08-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="string">&#x27;1998-09-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;1999-10-01&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="商品信息表"><a href="#商品信息表" class="headerlink" title="商品信息表"></a>商品信息表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> sku_info;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> sku_info(</span><br><span class="line">    `sku_id`      string COMMENT <span class="string">&#x27;商品id&#x27;</span>,</span><br><span class="line">    `name`        string COMMENT <span class="string">&#x27;商品名称&#x27;</span>,</span><br><span class="line">    `category_id` string COMMENT <span class="string">&#x27;所属分类id&#x27;</span>,</span><br><span class="line">    `from_date`   string COMMENT <span class="string">&#x27;上架日期&#x27;</span>,</span><br><span class="line">    `price`       <span class="keyword">double</span> COMMENT <span class="string">&#x27;商品单价&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;商品属性表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> sku_info</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;xiaomi 10&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2020-01-01&#x27;</span>, <span class="number">2000</span>),</span><br><span class="line">       (<span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;手机壳&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2020-02-01&#x27;</span>, <span class="number">10</span>),</span><br><span class="line">       (<span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;apple 12&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2020-03-01&#x27;</span>, <span class="number">5000</span>),</span><br><span class="line">       (<span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;xiaomi 13&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2020-04-01&#x27;</span>, <span class="number">6000</span>),</span><br><span class="line">       (<span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;破壁机&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2020-01-01&#x27;</span>, <span class="number">500</span>),</span><br><span class="line">       (<span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;洗碗机&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2020-02-01&#x27;</span>, <span class="number">2000</span>),</span><br><span class="line">       (<span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;热水壶&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2020-03-01&#x27;</span>, <span class="number">100</span>),</span><br><span class="line">       (<span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;微波炉&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2020-04-01&#x27;</span>, <span class="number">600</span>),</span><br><span class="line">       (<span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;自行车&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2020-01-01&#x27;</span>, <span class="number">1000</span>),</span><br><span class="line">       (<span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;帐篷&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2020-02-01&#x27;</span>, <span class="number">100</span>),</span><br><span class="line">       (<span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;烧烤架&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2020-02-01&#x27;</span>, <span class="number">50</span>),</span><br><span class="line">       (<span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;遮阳伞&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2020-03-01&#x27;</span>, <span class="number">20</span>);</span><br></pre></td></tr></table></figure>

<h4 id="商品分类信息表"><a href="#商品分类信息表" class="headerlink" title="商品分类信息表"></a>商品分类信息表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> category_info;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> category_info(</span><br><span class="line">    `category_id`   string,</span><br><span class="line">    `category_name` string</span><br><span class="line">) COMMENT <span class="string">&#x27;品类表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> category_info</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;1&#x27;</span>,<span class="string">&#x27;数码&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;2&#x27;</span>,<span class="string">&#x27;厨卫&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;3&#x27;</span>,<span class="string">&#x27;户外&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="订单信息表"><a href="#订单信息表" class="headerlink" title="订单信息表"></a>订单信息表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> order_info;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> order_info(</span><br><span class="line">    `order_id`     string COMMENT <span class="string">&#x27;订单id&#x27;</span>,</span><br><span class="line">    `user_id`      string COMMENT <span class="string">&#x27;用户id&#x27;</span>,</span><br><span class="line">    `create_date`  string COMMENT <span class="string">&#x27;下单日期&#x27;</span>,</span><br><span class="line">    `total_amount` <span class="type">decimal</span>(<span class="number">16</span>, <span class="number">2</span>) COMMENT <span class="string">&#x27;订单总金额&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;订单表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> order_info</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;2021-09-27&#x27;</span>, <span class="number">29000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>, <span class="number">70500.00</span>),</span><br><span class="line">       (<span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>, <span class="number">43300.00</span>),</span><br><span class="line">       (<span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;2021-09-30&#x27;</span>, <span class="number">860.00</span>),</span><br><span class="line">       (<span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">46180.00</span>),</span><br><span class="line">       (<span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">50000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">75500.00</span>),</span><br><span class="line">       (<span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">6170.00</span>),</span><br><span class="line">       (<span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">18580.00</span>),</span><br><span class="line">       (<span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">28000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">23400.00</span>),</span><br><span class="line">       (<span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">5910.00</span>),</span><br><span class="line">       (<span class="string">&#x27;13&#x27;</span>, <span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">13000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;14&#x27;</span>, <span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">69500.00</span>),</span><br><span class="line">       (<span class="string">&#x27;15&#x27;</span>, <span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">2000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;16&#x27;</span>, <span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">5380.00</span>),</span><br><span class="line">       (<span class="string">&#x27;17&#x27;</span>, <span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">6210.00</span>),</span><br><span class="line">       (<span class="string">&#x27;18&#x27;</span>, <span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">68000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;19&#x27;</span>, <span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">43100.00</span>),</span><br><span class="line">       (<span class="string">&#x27;20&#x27;</span>, <span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">2790.00</span>),</span><br><span class="line">       (<span class="string">&#x27;21&#x27;</span>, <span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">9390.00</span>),</span><br><span class="line">       (<span class="string">&#x27;22&#x27;</span>, <span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">58000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;23&#x27;</span>, <span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">46600.00</span>),</span><br><span class="line">       (<span class="string">&#x27;24&#x27;</span>, <span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">5160.00</span>),</span><br><span class="line">       (<span class="string">&#x27;25&#x27;</span>, <span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">55350.00</span>),</span><br><span class="line">       (<span class="string">&#x27;26&#x27;</span>, <span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">14500.00</span>),</span><br><span class="line">       (<span class="string">&#x27;27&#x27;</span>, <span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">47400.00</span>),</span><br><span class="line">       (<span class="string">&#x27;28&#x27;</span>, <span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">6900.00</span>),</span><br><span class="line">       (<span class="string">&#x27;29&#x27;</span>, <span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">56570.00</span>),</span><br><span class="line">       (<span class="string">&#x27;30&#x27;</span>, <span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">44500.00</span>),</span><br><span class="line">       (<span class="string">&#x27;31&#x27;</span>, <span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">50800.00</span>),</span><br><span class="line">       (<span class="string">&#x27;32&#x27;</span>, <span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">3900.00</span>),</span><br><span class="line">       (<span class="string">&#x27;33&#x27;</span>, <span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">41480.00</span>),</span><br><span class="line">       (<span class="string">&#x27;34&#x27;</span>, <span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">88000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;35&#x27;</span>, <span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">15000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;36&#x27;</span>, <span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">9020.00</span>),</span><br><span class="line">       (<span class="string">&#x27;37&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">9260.00</span>),</span><br><span class="line">       (<span class="string">&#x27;38&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">12000.00</span>),</span><br><span class="line">       (<span class="string">&#x27;39&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">23900.00</span>),</span><br><span class="line">       (<span class="string">&#x27;40&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">6790.00</span>);</span><br></pre></td></tr></table></figure>

<h4 id="订单明细表"><a href="#订单明细表" class="headerlink" title="订单明细表"></a>订单明细表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> order_detail;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> order_detail</span><br><span class="line">(</span><br><span class="line">    `order_detail_id` string COMMENT <span class="string">&#x27;订单明细id&#x27;</span>,</span><br><span class="line">    `order_id`        string COMMENT <span class="string">&#x27;订单id&#x27;</span>,</span><br><span class="line">    `sku_id`          string COMMENT <span class="string">&#x27;商品id&#x27;</span>,</span><br><span class="line">    `create_date`     string COMMENT <span class="string">&#x27;下单日期&#x27;</span>,</span><br><span class="line">    `price`           <span class="type">decimal</span>(<span class="number">16</span>, <span class="number">2</span>) COMMENT <span class="string">&#x27;下单时的商品单价&#x27;</span>,</span><br><span class="line">    `sku_num`         <span class="type">int</span> COMMENT <span class="string">&#x27;下单商品件数&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;订单明细表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">INSERT</span> overwrite <span class="keyword">table</span> order_detail</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-09-27&#x27;</span>, <span class="number">2000.00</span>, <span class="number">2</span>),</span><br><span class="line">       (<span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-09-27&#x27;</span>, <span class="number">5000.00</span>, <span class="number">5</span>),</span><br><span class="line">       (<span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>, <span class="number">6000.00</span>, <span class="number">9</span>),</span><br><span class="line">       (<span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>, <span class="number">500.00</span>, <span class="number">33</span>),</span><br><span class="line">       (<span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>, <span class="number">100.00</span>, <span class="number">37</span>),</span><br><span class="line">       (<span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>, <span class="number">600.00</span>, <span class="number">46</span>),</span><br><span class="line">       (<span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>, <span class="number">1000.00</span>, <span class="number">12</span>),</span><br><span class="line">       (<span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-09-30&#x27;</span>, <span class="number">20.00</span>, <span class="number">43</span>),</span><br><span class="line">       (<span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">2000.00</span>, <span class="number">8</span>),</span><br><span class="line">       (<span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">10.00</span>, <span class="number">18</span>),</span><br><span class="line">       (<span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">5000.00</span>, <span class="number">6</span>),</span><br><span class="line">       (<span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">6000.00</span>, <span class="number">8</span>),</span><br><span class="line">       (<span class="string">&#x27;13&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">2000.00</span>, <span class="number">1</span>),</span><br><span class="line">       (<span class="string">&#x27;14&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">100.00</span>, <span class="number">17</span>),</span><br><span class="line">       (<span class="string">&#x27;15&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">600.00</span>, <span class="number">48</span>),</span><br><span class="line">       (<span class="string">&#x27;16&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="number">1000.00</span>, <span class="number">45</span>),</span><br><span class="line">       (<span class="string">&#x27;17&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">100.00</span>, <span class="number">48</span>),</span><br><span class="line">       (<span class="string">&#x27;18&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">50.00</span>, <span class="number">15</span>),</span><br><span class="line">       (<span class="string">&#x27;19&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">20.00</span>, <span class="number">31</span>),</span><br><span class="line">       (<span class="string">&#x27;20&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-09-30&#x27;</span>, <span class="number">2000.00</span>, <span class="number">9</span>),</span><br><span class="line">       (<span class="string">&#x27;21&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">10.00</span>, <span class="number">5800</span>),</span><br><span class="line">       (<span class="string">&#x27;22&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">6000.00</span>, <span class="number">1</span>),</span><br><span class="line">       (<span class="string">&#x27;23&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">500.00</span>, <span class="number">24</span>),</span><br><span class="line">       (<span class="string">&#x27;24&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">2000.00</span>, <span class="number">5</span>),</span><br><span class="line">       (<span class="string">&#x27;25&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="number">600.00</span>, <span class="number">39</span>),</span><br><span class="line">       (<span class="string">&#x27;26&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">100.00</span>, <span class="number">47</span>),</span><br><span class="line">       (<span class="string">&#x27;27&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">50.00</span>, <span class="number">19</span>),</span><br><span class="line">       (<span class="string">&#x27;28&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">20.00</span>, <span class="number">13000</span>),</span><br><span class="line">       (<span class="string">&#x27;29&#x27;</span>, <span class="string">&#x27;13&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">2000.00</span>, <span class="number">4</span>),</span><br><span class="line">       (<span class="string">&#x27;30&#x27;</span>, <span class="string">&#x27;13&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">5000.00</span>, <span class="number">1</span>),</span><br><span class="line">       (<span class="string">&#x27;31&#x27;</span>, <span class="string">&#x27;14&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">6000.00</span>, <span class="number">5</span>),</span><br><span class="line">       (<span class="string">&#x27;32&#x27;</span>, <span class="string">&#x27;14&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">500.00</span>, <span class="number">47</span>),</span><br><span class="line">       (<span class="string">&#x27;33&#x27;</span>, <span class="string">&#x27;14&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">2000.00</span>, <span class="number">8</span>),</span><br><span class="line">       (<span class="string">&#x27;34&#x27;</span>, <span class="string">&#x27;15&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">100.00</span>, <span class="number">20</span>),</span><br><span class="line">       (<span class="string">&#x27;35&#x27;</span>, <span class="string">&#x27;16&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">100.00</span>, <span class="number">22</span>),</span><br><span class="line">       (<span class="string">&#x27;36&#x27;</span>, <span class="string">&#x27;16&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">50.00</span>, <span class="number">42</span>),</span><br><span class="line">       (<span class="string">&#x27;37&#x27;</span>, <span class="string">&#x27;16&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="number">20.00</span>, <span class="number">7400</span>),</span><br><span class="line">       (<span class="string">&#x27;38&#x27;</span>, <span class="string">&#x27;17&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">2000.00</span>, <span class="number">3</span>),</span><br><span class="line">       (<span class="string">&#x27;39&#x27;</span>, <span class="string">&#x27;17&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">10.00</span>, <span class="number">21</span>),</span><br><span class="line">       (<span class="string">&#x27;40&#x27;</span>, <span class="string">&#x27;18&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">6000.00</span>, <span class="number">8</span>),</span><br><span class="line">       (<span class="string">&#x27;41&#x27;</span>, <span class="string">&#x27;18&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">500.00</span>, <span class="number">28</span>),</span><br><span class="line">       (<span class="string">&#x27;42&#x27;</span>, <span class="string">&#x27;18&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">2000.00</span>, <span class="number">3</span>),</span><br><span class="line">       (<span class="string">&#x27;43&#x27;</span>, <span class="string">&#x27;19&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">100.00</span>, <span class="number">55</span>),</span><br><span class="line">       (<span class="string">&#x27;44&#x27;</span>, <span class="string">&#x27;19&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">600.00</span>, <span class="number">11</span>),</span><br><span class="line">       (<span class="string">&#x27;45&#x27;</span>, <span class="string">&#x27;19&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">1000.00</span>, <span class="number">31</span>),</span><br><span class="line">       (<span class="string">&#x27;46&#x27;</span>, <span class="string">&#x27;20&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">50.00</span>, <span class="number">45</span>),</span><br><span class="line">       (<span class="string">&#x27;47&#x27;</span>, <span class="string">&#x27;20&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">20.00</span>, <span class="number">27</span>),</span><br><span class="line">       (<span class="string">&#x27;48&#x27;</span>, <span class="string">&#x27;21&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">2000.00</span>, <span class="number">2</span>),</span><br><span class="line">       (<span class="string">&#x27;49&#x27;</span>, <span class="string">&#x27;21&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">10.00</span>, <span class="number">39</span>),</span><br><span class="line">       (<span class="string">&#x27;50&#x27;</span>, <span class="string">&#x27;21&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="number">5000.00</span>, <span class="number">1</span>),</span><br><span class="line">       (<span class="string">&#x27;51&#x27;</span>, <span class="string">&#x27;22&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">6000.00</span>, <span class="number">8</span>),</span><br><span class="line">       (<span class="string">&#x27;52&#x27;</span>, <span class="string">&#x27;22&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">500.00</span>, <span class="number">20</span>),</span><br><span class="line">       (<span class="string">&#x27;53&#x27;</span>, <span class="string">&#x27;23&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">100.00</span>, <span class="number">58</span>),</span><br><span class="line">       (<span class="string">&#x27;54&#x27;</span>, <span class="string">&#x27;23&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">600.00</span>, <span class="number">18</span>),</span><br><span class="line">       (<span class="string">&#x27;55&#x27;</span>, <span class="string">&#x27;23&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">1000.00</span>, <span class="number">30</span>),</span><br><span class="line">       (<span class="string">&#x27;56&#x27;</span>, <span class="string">&#x27;24&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">100.00</span>, <span class="number">27</span>),</span><br><span class="line">       (<span class="string">&#x27;57&#x27;</span>, <span class="string">&#x27;24&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">50.00</span>, <span class="number">28</span>),</span><br><span class="line">       (<span class="string">&#x27;58&#x27;</span>, <span class="string">&#x27;24&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">20.00</span>, <span class="number">53</span>),</span><br><span class="line">       (<span class="string">&#x27;59&#x27;</span>, <span class="string">&#x27;25&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">2000.00</span>, <span class="number">5</span>),</span><br><span class="line">       (<span class="string">&#x27;60&#x27;</span>, <span class="string">&#x27;25&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">10.00</span>, <span class="number">35</span>),</span><br><span class="line">       (<span class="string">&#x27;61&#x27;</span>, <span class="string">&#x27;25&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">5000.00</span>, <span class="number">9</span>),</span><br><span class="line">       (<span class="string">&#x27;62&#x27;</span>, <span class="string">&#x27;26&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">6000.00</span>, <span class="number">1</span>),</span><br><span class="line">       (<span class="string">&#x27;63&#x27;</span>, <span class="string">&#x27;26&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">500.00</span>, <span class="number">13</span>),</span><br><span class="line">       (<span class="string">&#x27;64&#x27;</span>, <span class="string">&#x27;26&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="number">2000.00</span>, <span class="number">1</span>),</span><br><span class="line">       (<span class="string">&#x27;65&#x27;</span>, <span class="string">&#x27;27&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">100.00</span>, <span class="number">30</span>),</span><br><span class="line">       (<span class="string">&#x27;66&#x27;</span>, <span class="string">&#x27;27&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">600.00</span>, <span class="number">19</span>),</span><br><span class="line">       (<span class="string">&#x27;67&#x27;</span>, <span class="string">&#x27;27&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">1000.00</span>, <span class="number">33</span>),</span><br><span class="line">       (<span class="string">&#x27;68&#x27;</span>, <span class="string">&#x27;28&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">100.00</span>, <span class="number">37</span>),</span><br><span class="line">       (<span class="string">&#x27;69&#x27;</span>, <span class="string">&#x27;28&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">50.00</span>, <span class="number">46</span>),</span><br><span class="line">       (<span class="string">&#x27;70&#x27;</span>, <span class="string">&#x27;28&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">20.00</span>, <span class="number">45</span>),</span><br><span class="line">       (<span class="string">&#x27;71&#x27;</span>, <span class="string">&#x27;29&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">2000.00</span>, <span class="number">8</span>),</span><br><span class="line">       (<span class="string">&#x27;72&#x27;</span>, <span class="string">&#x27;29&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">10.00</span>, <span class="number">57</span>),</span><br><span class="line">       (<span class="string">&#x27;73&#x27;</span>, <span class="string">&#x27;29&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">5000.00</span>, <span class="number">8</span>),</span><br><span class="line">       (<span class="string">&#x27;74&#x27;</span>, <span class="string">&#x27;30&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">6000.00</span>, <span class="number">3</span>),</span><br><span class="line">       (<span class="string">&#x27;75&#x27;</span>, <span class="string">&#x27;30&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">500.00</span>, <span class="number">33</span>),</span><br><span class="line">       (<span class="string">&#x27;76&#x27;</span>, <span class="string">&#x27;30&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="number">2000.00</span>, <span class="number">5</span>),</span><br><span class="line">       (<span class="string">&#x27;77&#x27;</span>, <span class="string">&#x27;31&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">600.00</span>, <span class="number">13</span>),</span><br><span class="line">       (<span class="string">&#x27;78&#x27;</span>, <span class="string">&#x27;31&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">1000.00</span>, <span class="number">43</span>),</span><br><span class="line">       (<span class="string">&#x27;79&#x27;</span>, <span class="string">&#x27;32&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">100.00</span>, <span class="number">24</span>),</span><br><span class="line">       (<span class="string">&#x27;80&#x27;</span>, <span class="string">&#x27;32&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">50.00</span>, <span class="number">30</span>),</span><br><span class="line">       (<span class="string">&#x27;81&#x27;</span>, <span class="string">&#x27;33&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">2000.00</span>, <span class="number">8</span>),</span><br><span class="line">       (<span class="string">&#x27;82&#x27;</span>, <span class="string">&#x27;33&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">10.00</span>, <span class="number">48</span>),</span><br><span class="line">       (<span class="string">&#x27;83&#x27;</span>, <span class="string">&#x27;33&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">5000.00</span>, <span class="number">5</span>),</span><br><span class="line">       (<span class="string">&#x27;84&#x27;</span>, <span class="string">&#x27;34&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">6000.00</span>, <span class="number">10</span>),</span><br><span class="line">       (<span class="string">&#x27;85&#x27;</span>, <span class="string">&#x27;34&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">500.00</span>, <span class="number">44</span>),</span><br><span class="line">       (<span class="string">&#x27;86&#x27;</span>, <span class="string">&#x27;34&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="number">2000.00</span>, <span class="number">3</span>),</span><br><span class="line">       (<span class="string">&#x27;87&#x27;</span>, <span class="string">&#x27;35&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">600.00</span>, <span class="number">25</span>),</span><br><span class="line">       (<span class="string">&#x27;88&#x27;</span>, <span class="string">&#x27;36&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">100.00</span>, <span class="number">57</span>),</span><br><span class="line">       (<span class="string">&#x27;89&#x27;</span>, <span class="string">&#x27;36&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">50.00</span>, <span class="number">44</span>),</span><br><span class="line">       (<span class="string">&#x27;90&#x27;</span>, <span class="string">&#x27;36&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">20.00</span>, <span class="number">56</span>),</span><br><span class="line">       (<span class="string">&#x27;91&#x27;</span>, <span class="string">&#x27;37&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">2000.00</span>, <span class="number">2</span>),</span><br><span class="line">       (<span class="string">&#x27;92&#x27;</span>, <span class="string">&#x27;37&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">10.00</span>, <span class="number">26</span>),</span><br><span class="line">       (<span class="string">&#x27;93&#x27;</span>, <span class="string">&#x27;37&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">5000.00</span>, <span class="number">1</span>),</span><br><span class="line">       (<span class="string">&#x27;94&#x27;</span>, <span class="string">&#x27;38&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">2000.00</span>, <span class="number">6</span>),</span><br><span class="line">       (<span class="string">&#x27;95&#x27;</span>, <span class="string">&#x27;39&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">100.00</span>, <span class="number">35</span>),</span><br><span class="line">       (<span class="string">&#x27;96&#x27;</span>, <span class="string">&#x27;39&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">600.00</span>, <span class="number">34</span>),</span><br><span class="line">       (<span class="string">&#x27;97&#x27;</span>, <span class="string">&#x27;40&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">100.00</span>, <span class="number">37</span>),</span><br><span class="line">       (<span class="string">&#x27;98&#x27;</span>, <span class="string">&#x27;40&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">50.00</span>, <span class="number">51</span>),</span><br><span class="line">       (<span class="string">&#x27;99&#x27;</span>, <span class="string">&#x27;40&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2020-10-08&#x27;</span>, <span class="number">20.00</span>, <span class="number">27</span>);</span><br></pre></td></tr></table></figure>

<h4 id="登录明细表"><a href="#登录明细表" class="headerlink" title="登录明细表"></a>登录明细表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> user_login_detail;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> user_login_detail</span><br><span class="line">(</span><br><span class="line">    `user_id`    string comment <span class="string">&#x27;用户id&#x27;</span>,</span><br><span class="line">    `ip_address` string comment <span class="string">&#x27;ip地址&#x27;</span>,</span><br><span class="line">    `login_ts`   string comment <span class="string">&#x27;登录时间&#x27;</span>,</span><br><span class="line">    `logout_ts`  string comment <span class="string">&#x27;登出时间&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;用户登录明细表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">INSERT</span> overwrite <span class="keyword">table</span> user_login_detail</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;180.149.130.161&#x27;</span>, <span class="string">&#x27;2021-09-21 08:00:00&#x27;</span>, <span class="string">&#x27;2021-09-27 08:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;180.149.130.161&#x27;</span>, <span class="string">&#x27;2021-09-27 08:00:00&#x27;</span>, <span class="string">&#x27;2021-09-27 08:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;180.149.130.161&#x27;</span>, <span class="string">&#x27;2021-09-28 09:00:00&#x27;</span>, <span class="string">&#x27;2021-09-28 09:10:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;180.149.130.161&#x27;</span>, <span class="string">&#x27;2021-09-29 13:30:00&#x27;</span>, <span class="string">&#x27;2021-09-29 13:50:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;180.149.130.161&#x27;</span>, <span class="string">&#x27;2021-09-30 20:00:00&#x27;</span>, <span class="string">&#x27;2021-09-30 20:10:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;120.245.11.2&#x27;</span>, <span class="string">&#x27;2021-09-22 09:00:00&#x27;</span>, <span class="string">&#x27;2021-09-27 09:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;120.245.11.2&#x27;</span>, <span class="string">&#x27;2021-10-01 08:00:00&#x27;</span>, <span class="string">&#x27;2021-10-01 08:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;180.149.130.174&#x27;</span>, <span class="string">&#x27;2021-10-01 07:50:00&#x27;</span>, <span class="string">&#x27;2021-10-01 08:20:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;120.245.11.2&#x27;</span>, <span class="string">&#x27;2021-10-02 08:00:00&#x27;</span>, <span class="string">&#x27;2021-10-02 08:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;27.184.97.3&#x27;</span>, <span class="string">&#x27;2021-09-23 10:00:00&#x27;</span>, <span class="string">&#x27;2021-09-27 10:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;27.184.97.3&#x27;</span>, <span class="string">&#x27;2021-10-03 07:50:00&#x27;</span>, <span class="string">&#x27;2021-10-03 09:20:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;27.184.97.34&#x27;</span>, <span class="string">&#x27;2021-09-24 11:00:00&#x27;</span>, <span class="string">&#x27;2021-09-27 11:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;27.184.97.34&#x27;</span>, <span class="string">&#x27;2021-10-03 07:50:00&#x27;</span>, <span class="string">&#x27;2021-10-03 08:20:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;27.184.97.34&#x27;</span>, <span class="string">&#x27;2021-10-03 08:50:00&#x27;</span>, <span class="string">&#x27;2021-10-03 10:20:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;120.245.11.89&#x27;</span>, <span class="string">&#x27;2021-10-03 08:40:00&#x27;</span>, <span class="string">&#x27;2021-10-03 10:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;119.180.192.212&#x27;</span>, <span class="string">&#x27;2021-10-04 09:10:00&#x27;</span>, <span class="string">&#x27;2021-10-04 09:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;119.180.192.66&#x27;</span>, <span class="string">&#x27;2021-10-04 08:40:00&#x27;</span>, <span class="string">&#x27;2021-10-04 10:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;119.180.192.66&#x27;</span>, <span class="string">&#x27;2021-10-05 21:50:00&#x27;</span>, <span class="string">&#x27;2021-10-05 22:40:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;219.134.104.7&#x27;</span>, <span class="string">&#x27;2021-09-25 12:00:00&#x27;</span>, <span class="string">&#x27;2021-09-27 12:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;219.134.104.7&#x27;</span>, <span class="string">&#x27;2021-10-05 22:00:00&#x27;</span>, <span class="string">&#x27;2021-10-05 23:00:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;219.134.104.7&#x27;</span>, <span class="string">&#x27;2021-10-06 09:10:00&#x27;</span>, <span class="string">&#x27;2021-10-06 10:20:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;27.184.97.46&#x27;</span>, <span class="string">&#x27;2021-10-06 09:00:00&#x27;</span>, <span class="string">&#x27;2021-10-06 10:00:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;101.227.131.22&#x27;</span>, <span class="string">&#x27;2021-10-06 09:00:00&#x27;</span>, <span class="string">&#x27;2021-10-06 10:00:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;101.227.131.22&#x27;</span>, <span class="string">&#x27;2021-10-06 22:00:00&#x27;</span>, <span class="string">&#x27;2021-10-06 23:00:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;101.227.131.29&#x27;</span>, <span class="string">&#x27;2021-09-26 13:00:00&#x27;</span>, <span class="string">&#x27;2021-09-27 13:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;101.227.131.29&#x27;</span>, <span class="string">&#x27;2021-10-06 08:50:00&#x27;</span>, <span class="string">&#x27;2021-10-06 10:20:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;101.227.131.29&#x27;</span>, <span class="string">&#x27;2021-10-08 09:00:00&#x27;</span>, <span class="string">&#x27;2021-10-08 09:10:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;119.180.192.10&#x27;</span>, <span class="string">&#x27;2021-09-27 14:00:00&#x27;</span>, <span class="string">&#x27;2021-09-27 14:30:00&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;119.180.192.10&#x27;</span>, <span class="string">&#x27;2021-10-09 08:50:00&#x27;</span>, <span class="string">&#x27;2021-10-09 10:20:00&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="商品价格变更明细表"><a href="#商品价格变更明细表" class="headerlink" title="商品价格变更明细表"></a>商品价格变更明细表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> sku_price_modify_detail;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> sku_price_modify_detail</span><br><span class="line">(</span><br><span class="line">    `sku_id`      string comment <span class="string">&#x27;商品id&#x27;</span>,</span><br><span class="line">    `new_price`   <span class="type">decimal</span>(<span class="number">16</span>, <span class="number">2</span>) comment <span class="string">&#x27;更改后的价格&#x27;</span>,</span><br><span class="line">    `change_date` string comment <span class="string">&#x27;变动日期&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;商品价格变更明细表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> sku_price_modify_detail</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;1&#x27;</span>, <span class="number">1900</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1&#x27;</span>, <span class="number">2000</span>, <span class="string">&#x27;2021-09-26&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;2&#x27;</span>, <span class="number">80</span>, <span class="string">&#x27;2021-09-29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;2&#x27;</span>, <span class="number">10</span>, <span class="string">&#x27;2021-09-30&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;3&#x27;</span>, <span class="number">4999</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;3&#x27;</span>, <span class="number">5000</span>, <span class="string">&#x27;2021-09-26&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;4&#x27;</span>, <span class="number">5600</span>, <span class="string">&#x27;2021-09-26&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;4&#x27;</span>, <span class="number">6000</span>, <span class="string">&#x27;2021-09-27&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;5&#x27;</span>, <span class="number">490</span>, <span class="string">&#x27;2021-09-27&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;5&#x27;</span>, <span class="number">500</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;6&#x27;</span>, <span class="number">1988</span>, <span class="string">&#x27;2021-09-30&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;6&#x27;</span>, <span class="number">2000</span>, <span class="string">&#x27;2021-10-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;7&#x27;</span>, <span class="number">88</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;7&#x27;</span>, <span class="number">100</span>, <span class="string">&#x27;2021-09-29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;8&#x27;</span>, <span class="number">800</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;8&#x27;</span>, <span class="number">600</span>, <span class="string">&#x27;2021-09-29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;9&#x27;</span>, <span class="number">1100</span>, <span class="string">&#x27;2021-09-27&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;9&#x27;</span>, <span class="number">1000</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;10&#x27;</span>, <span class="number">90</span>, <span class="string">&#x27;2021-10-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;10&#x27;</span>, <span class="number">100</span>, <span class="string">&#x27;2021-10-02&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;11&#x27;</span>, <span class="number">66</span>, <span class="string">&#x27;2021-10-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;11&#x27;</span>, <span class="number">50</span>, <span class="string">&#x27;2021-10-02&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;12&#x27;</span>, <span class="number">35</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;12&#x27;</span>, <span class="number">20</span>, <span class="string">&#x27;2021-09-29&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="配送信息表"><a href="#配送信息表" class="headerlink" title="配送信息表"></a>配送信息表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> delivery_info;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> delivery_info</span><br><span class="line">(</span><br><span class="line">    `delivery_id` string comment <span class="string">&#x27;配送单id&#x27;</span>,</span><br><span class="line">    `order_id`    string comment <span class="string">&#x27;订单id&#x27;</span>,</span><br><span class="line">    `user_id`     string comment <span class="string">&#x27;用户id&#x27;</span>,</span><br><span class="line">    `order_date`  string comment <span class="string">&#x27;下单日期&#x27;</span>,</span><br><span class="line">    `custom_date` string comment <span class="string">&#x27;期望配送日期&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;邮寄信息表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> delivery_info</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;2021-09-27&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>, <span class="string">&#x27;2021-09-30&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;2021-09-30&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>, <span class="string">&#x27;2021-10-02&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;13&#x27;</span>, <span class="string">&#x27;13&#x27;</span>, <span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;14&#x27;</span>, <span class="string">&#x27;14&#x27;</span>, <span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;15&#x27;</span>, <span class="string">&#x27;15&#x27;</span>, <span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;16&#x27;</span>, <span class="string">&#x27;16&#x27;</span>, <span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>, <span class="string">&#x27;2021-10-03&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;17&#x27;</span>, <span class="string">&#x27;17&#x27;</span>, <span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;18&#x27;</span>, <span class="string">&#x27;18&#x27;</span>, <span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;19&#x27;</span>, <span class="string">&#x27;19&#x27;</span>, <span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;20&#x27;</span>, <span class="string">&#x27;20&#x27;</span>, <span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;21&#x27;</span>, <span class="string">&#x27;21&#x27;</span>, <span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;22&#x27;</span>, <span class="string">&#x27;22&#x27;</span>, <span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;23&#x27;</span>, <span class="string">&#x27;23&#x27;</span>, <span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;24&#x27;</span>, <span class="string">&#x27;24&#x27;</span>, <span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;25&#x27;</span>, <span class="string">&#x27;25&#x27;</span>, <span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;26&#x27;</span>, <span class="string">&#x27;26&#x27;</span>, <span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;27&#x27;</span>, <span class="string">&#x27;27&#x27;</span>, <span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;28&#x27;</span>, <span class="string">&#x27;28&#x27;</span>, <span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;29&#x27;</span>, <span class="string">&#x27;29&#x27;</span>, <span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;30&#x27;</span>, <span class="string">&#x27;30&#x27;</span>, <span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;31&#x27;</span>, <span class="string">&#x27;31&#x27;</span>, <span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="string">&#x27;2021-10-09&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;32&#x27;</span>, <span class="string">&#x27;32&#x27;</span>, <span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="string">&#x27;2021-10-09&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;33&#x27;</span>, <span class="string">&#x27;33&#x27;</span>, <span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;34&#x27;</span>, <span class="string">&#x27;34&#x27;</span>, <span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;35&#x27;</span>, <span class="string">&#x27;35&#x27;</span>, <span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>, <span class="string">&#x27;2021-10-10&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;36&#x27;</span>, <span class="string">&#x27;36&#x27;</span>, <span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>, <span class="string">&#x27;2021-10-09&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;37&#x27;</span>, <span class="string">&#x27;37&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>, <span class="string">&#x27;2021-10-10&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;38&#x27;</span>, <span class="string">&#x27;38&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>, <span class="string">&#x27;2021-10-10&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;39&#x27;</span>, <span class="string">&#x27;39&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>, <span class="string">&#x27;2021-10-09&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;40&#x27;</span>, <span class="string">&#x27;40&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>, <span class="string">&#x27;2021-10-09&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="好友关系表"><a href="#好友关系表" class="headerlink" title="好友关系表"></a>好友关系表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> friendship_info;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> friendship_info(</span><br><span class="line">    `user1_id` string comment <span class="string">&#x27;用户1id&#x27;</span>,</span><br><span class="line">    `user2_id` string comment <span class="string">&#x27;用户2id&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;用户关系表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> friendship_info</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;102&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;102&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;102&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;102&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;102&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;102&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;102&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;102&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;102&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;1010&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;108&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;106&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;104&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;102&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h4 id="收藏信息表"><a href="#收藏信息表" class="headerlink" title="收藏信息表"></a>收藏信息表</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--建表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> favor_info;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> favor_info</span><br><span class="line">(</span><br><span class="line">    `user_id`     string comment <span class="string">&#x27;用户id&#x27;</span>,</span><br><span class="line">    `sku_id`      string comment <span class="string">&#x27;商品id&#x27;</span>,</span><br><span class="line">    `create_date` string comment <span class="string">&#x27;收藏日期&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;用户收藏表&#x27;</span></span><br><span class="line">    <span class="type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="keyword">BY</span> <span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line"><span class="keyword">insert</span> overwrite <span class="keyword">table</span> favor_info</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-09-23&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-09-23&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-09-21&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;101&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-09-24&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-09-24&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-09-23&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-09-22&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-09-23&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-09-23&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;102&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-09-26&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-09-24&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-09-26&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-09-27&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-09-24&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;103&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-09-27&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-09-25&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;104&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-10-06&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-05&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;105&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;2021-10-04&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;106&#x27;</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-09-28&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;107&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-09-27&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-10-08&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;2021-10-10&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-10-10&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;108&#x27;</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;2021-10-07&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-09-27&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-09-30&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;109&#x27;</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;2021-09-26&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;2021-09-29&#x27;</span>),</span><br><span class="line">       (<span class="string">&#x27;1010&#x27;</span>, <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;2021-10-01&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h3 id="案例练习"><a href="#案例练习" class="headerlink" title="案例练习"></a>案例练习</h3><h4 id="查询累积销量排名第二的商品"><a href="#查询累积销量排名第二的商品" class="headerlink" title="查询累积销量排名第二的商品"></a>查询累积销量排名第二的商品</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> </span><br><span class="line">		t1.sku_id,</span><br><span class="line">		t1.sum_sku_num,</span><br><span class="line">		<span class="built_in">dense_rank</span>() <span class="keyword">over</span> (<span class="keyword">order</span> <span class="keyword">by</span> t1.sum_sku_num <span class="keyword">desc</span>) rk</span><br><span class="line">	<span class="keyword">from</span> (</span><br><span class="line">		<span class="keyword">select</span> od.sku_id, <span class="built_in">sum</span>(od.sku_num) sum_sku_num</span><br><span class="line">		<span class="keyword">from</span> order_detail od </span><br><span class="line">		<span class="keyword">group</span> <span class="keyword">by</span> od.sku_id <span class="comment">--聚合统计sku_id，和对应下单商品数量</span></span><br><span class="line">	) t1 <span class="comment">--按照下单商品数排序</span></span><br><span class="line">) t2</span><br><span class="line"><span class="keyword">where</span> t2.rk <span class="operator">=</span> <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">sku_id<span class="operator">|</span>sum_sku_num<span class="operator">|</span>rk<span class="operator">|</span></span><br><span class="line"><span class="comment">------+-----------+--+</span></span><br><span class="line"><span class="number">2</span>     <span class="operator">|</span>       <span class="number">6044</span><span class="operator">|</span> <span class="number">2</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h4 id="查询至少连续三天下单的用户"><a href="#查询至少连续三天下单的用户" class="headerlink" title="查询至少连续三天下单的用户"></a>查询至少连续三天下单的用户</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> t2.user_id</span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> </span><br><span class="line">		t1.user_id,</span><br><span class="line">		t1.create_date,</span><br><span class="line">		datediff(t1.create_date, <span class="built_in">lag</span>(t1.create_date, <span class="number">1</span>, <span class="string">&#x27;1970-01-01&#x27;</span>) <span class="keyword">over</span> (<span class="keyword">partition</span> <span class="keyword">by</span> t1.user_id <span class="keyword">order</span> <span class="keyword">by</span> t1.create_date)) flag</span><br><span class="line">	<span class="keyword">from</span> (</span><br><span class="line">		<span class="keyword">select</span> oi.user_id, oi.create_date </span><br><span class="line">		<span class="keyword">from</span> order_info oi </span><br><span class="line">		<span class="keyword">group</span> <span class="keyword">by</span> oi.user_id, oi.create_date <span class="comment">--同一天下单去重</span></span><br><span class="line">	) t1 <span class="comment">--当前行日期与前一天差值，第一天默认与1970-01-01做差值</span></span><br><span class="line">) t2</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> t2.user_id</span><br><span class="line"><span class="keyword">having</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="operator">&gt;=</span> <span class="number">3</span>; <span class="comment">--按用户分组，统计个数，大于等于3即至少连续三天下单</span></span><br><span class="line"></span><br><span class="line">user_id<span class="operator">|</span></span><br><span class="line"><span class="comment">-------+</span></span><br><span class="line"><span class="number">101</span>    <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h4 id="查询各品类商品中，销量最高的商品id，商品名，商品的销量，以及当前品类商品的数量"><a href="#查询各品类商品中，销量最高的商品id，商品名，商品的销量，以及当前品类商品的数量" class="headerlink" title="查询各品类商品中，销量最高的商品id，商品名，商品的销量，以及当前品类商品的数量"></a>查询各品类商品中，销量最高的商品id，商品名，商品的销量，以及当前品类商品的数量</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	t2.category_id,</span><br><span class="line">	t2.category_name,</span><br><span class="line">	t2.sku_id,</span><br><span class="line">	t2.name,</span><br><span class="line">	t2.order_num,</span><br><span class="line">	t2.sku_cnt</span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> </span><br><span class="line">		t1.sku_id,</span><br><span class="line">		t1.order_num,</span><br><span class="line">		si.name,</span><br><span class="line">		si.category_id,</span><br><span class="line">		ci.category_name,</span><br><span class="line">		<span class="built_in">rank</span>() <span class="keyword">over</span> (<span class="keyword">partition</span> <span class="keyword">by</span> ci.category_id <span class="keyword">order</span> <span class="keyword">by</span> t1.order_num <span class="keyword">desc</span>) rk,</span><br><span class="line">		<span class="built_in">count</span>(<span class="keyword">distinct</span> t1.sku_id) <span class="keyword">over</span> (<span class="keyword">partition</span> <span class="keyword">by</span> si.category_id) sku_cnt</span><br><span class="line">	<span class="keyword">from</span> (</span><br><span class="line">		<span class="keyword">select</span> od.sku_id, <span class="built_in">sum</span>(od.sku_num) order_num</span><br><span class="line">		<span class="keyword">from</span> order_detail od </span><br><span class="line">		<span class="keyword">group</span> <span class="keyword">by</span> od.sku_id </span><br><span class="line">	) t1</span><br><span class="line">	<span class="keyword">left</span> <span class="keyword">join</span> sku_info si <span class="keyword">on</span> t1.sku_id <span class="operator">=</span> si.sku_id </span><br><span class="line">	<span class="keyword">left</span> <span class="keyword">join</span> category_info ci <span class="keyword">on</span> si.category_id <span class="operator">=</span> ci.category_id </span><br><span class="line">) t2</span><br><span class="line"><span class="keyword">where</span> rk <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">category_id<span class="operator">|</span>category_name<span class="operator">|</span>       sku_id       <span class="operator">|</span>  name  <span class="operator">|</span>order_num<span class="operator">|</span>sku_cnt<span class="operator">|</span></span><br><span class="line">品类id     <span class="operator">|</span>   品类名称   <span class="operator">|</span>品类中销量最好的商品id<span class="operator">|</span>商品名称<span class="operator">|</span>   销量   <span class="operator">|</span>品类中商品数量<span class="operator">|</span></span><br><span class="line"><span class="comment">-----------+-------------+------+----+---------+-------+</span></span><br><span class="line"><span class="number">1</span>          <span class="operator">|</span>数码           <span class="operator">|</span>      <span class="number">2</span>           <span class="operator">|</span> 手机壳 <span class="operator">|</span>     <span class="number">6044</span><span class="operator">|</span>      <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">2</span>          <span class="operator">|</span>厨卫           <span class="operator">|</span>      <span class="number">8</span>           <span class="operator">|</span> 微波炉 <span class="operator">|</span>      <span class="number">253</span><span class="operator">|</span>      <span class="number">4</span><span class="operator">|</span></span><br><span class="line"><span class="number">3</span>          <span class="operator">|</span>户外           <span class="operator">|</span>      <span class="number">12</span>          <span class="operator">|</span> 遮阳伞 <span class="operator">|</span>    <span class="number">20682</span><span class="operator">|</span>      <span class="number">4</span><span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h4 id="查询用户的累计消费金额及VIP等级"><a href="#查询用户的累计消费金额及VIP等级" class="headerlink" title="查询用户的累计消费金额及VIP等级"></a>查询用户的累计消费金额及VIP等级</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">		t2.user_id,</span><br><span class="line">		t2.create_date,</span><br><span class="line">		t2.sum_so_far,</span><br><span class="line">		<span class="keyword">case</span> </span><br><span class="line">           <span class="keyword">when</span> sum_so_far <span class="operator">&gt;=</span> <span class="number">100000</span> <span class="keyword">then</span> <span class="string">&#x27;钻石会员&#x27;</span></span><br><span class="line">           <span class="keyword">when</span> sum_so_far <span class="operator">&gt;=</span> <span class="number">80000</span> <span class="keyword">then</span> <span class="string">&#x27;白金会员&#x27;</span></span><br><span class="line">           <span class="keyword">when</span> sum_so_far <span class="operator">&gt;=</span> <span class="number">50000</span> <span class="keyword">then</span> <span class="string">&#x27;黄金会员&#x27;</span></span><br><span class="line">           <span class="keyword">when</span> sum_so_far <span class="operator">&gt;=</span> <span class="number">30000</span> <span class="keyword">then</span> <span class="string">&#x27;白银会员&#x27;</span></span><br><span class="line">           <span class="keyword">when</span> sum_so_far <span class="operator">&gt;=</span> <span class="number">10000</span> <span class="keyword">then</span> <span class="string">&#x27;青铜会员&#x27;</span></span><br><span class="line">           <span class="keyword">when</span> sum_so_far <span class="operator">&gt;=</span> <span class="number">0</span> <span class="keyword">then</span> <span class="string">&#x27;普通会员&#x27;</span></span><br><span class="line">           <span class="keyword">end</span> vip_level</span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> </span><br><span class="line">		t1.user_id,</span><br><span class="line">		t1.create_date,</span><br><span class="line">		<span class="built_in">sum</span>(t1.sum_total_amount) <span class="keyword">over</span> (<span class="keyword">partition</span> <span class="keyword">by</span> t1.user_id <span class="keyword">order</span> <span class="keyword">by</span> t1.create_date) sum_so_far</span><br><span class="line">	<span class="keyword">from</span> (</span><br><span class="line">		<span class="keyword">select</span> </span><br><span class="line">			oi.user_id,</span><br><span class="line">			oi.create_date,</span><br><span class="line">			<span class="built_in">sum</span>(oi.total_amount) sum_total_amount</span><br><span class="line">		<span class="keyword">from</span> order_info oi </span><br><span class="line">		<span class="keyword">group</span> <span class="keyword">by</span> oi.user_id, oi.create_date </span><br><span class="line">	) t1</span><br><span class="line">) t2;</span><br><span class="line"></span><br><span class="line">user_id<span class="operator">|</span>create_date<span class="operator">|</span>sum_so_far<span class="operator">|</span>vip_level<span class="operator">|</span></span><br><span class="line"><span class="comment">-------+-----------+----------+---------+</span></span><br><span class="line"><span class="number">101</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-09</span><span class="number">-27</span> <span class="operator">|</span>  <span class="number">29000.00</span><span class="operator">|</span>青铜会员     <span class="operator">|</span></span><br><span class="line"><span class="number">101</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-09</span><span class="number">-28</span> <span class="operator">|</span>  <span class="number">99500.00</span><span class="operator">|</span>白金会员     <span class="operator">|</span></span><br><span class="line"><span class="number">101</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-09</span><span class="number">-29</span> <span class="operator">|</span> <span class="number">142800.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br><span class="line"><span class="number">101</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-09</span><span class="number">-30</span> <span class="operator">|</span> <span class="number">143660.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br><span class="line"><span class="number">1010</span>   <span class="operator">|</span><span class="number">2020</span><span class="number">-10</span><span class="number">-08</span> <span class="operator">|</span>  <span class="number">51950.00</span><span class="operator">|</span>黄金会员     <span class="operator">|</span></span><br><span class="line"><span class="number">102</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-01</span> <span class="operator">|</span> <span class="number">171680.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br><span class="line"><span class="number">102</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-02</span> <span class="operator">|</span> <span class="number">177850.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br><span class="line"><span class="number">103</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-02</span> <span class="operator">|</span>  <span class="number">69980.00</span><span class="operator">|</span>黄金会员     <span class="operator">|</span></span><br><span class="line"><span class="number">103</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-03</span> <span class="operator">|</span>  <span class="number">75890.00</span><span class="operator">|</span>黄金会员     <span class="operator">|</span></span><br><span class="line"><span class="number">104</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-03</span> <span class="operator">|</span>  <span class="number">89880.00</span><span class="operator">|</span>白金会员     <span class="operator">|</span></span><br><span class="line"><span class="number">105</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-04</span> <span class="operator">|</span> <span class="number">120100.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br><span class="line"><span class="number">106</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-04</span> <span class="operator">|</span>   <span class="number">9390.00</span><span class="operator">|</span>普通会员     <span class="operator">|</span></span><br><span class="line"><span class="number">106</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-05</span> <span class="operator">|</span> <span class="number">119150.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br><span class="line"><span class="number">107</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-05</span> <span class="operator">|</span>  <span class="number">69850.00</span><span class="operator">|</span>黄金会员     <span class="operator">|</span></span><br><span class="line"><span class="number">107</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-06</span> <span class="operator">|</span> <span class="number">124150.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br><span class="line"><span class="number">108</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-06</span> <span class="operator">|</span> <span class="number">101070.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br><span class="line"><span class="number">108</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-07</span> <span class="operator">|</span> <span class="number">155770.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br><span class="line"><span class="number">109</span>    <span class="operator">|</span><span class="number">2020</span><span class="number">-10</span><span class="number">-08</span> <span class="operator">|</span>  <span class="number">24020.00</span><span class="operator">|</span>青铜会员     <span class="operator">|</span></span><br><span class="line"><span class="number">109</span>    <span class="operator">|</span><span class="number">2021</span><span class="number">-10</span><span class="number">-07</span> <span class="operator">|</span> <span class="number">153500.00</span><span class="operator">|</span>钻石会员     <span class="operator">|</span></span><br></pre></td></tr></table></figure>

<h4 id="查询首次下单后第二天连续下单的用户比率"><a href="#查询首次下单后第二天连续下单的用户比率" class="headerlink" title="查询首次下单后第二天连续下单的用户比率"></a>查询首次下单后第二天连续下单的用户比率</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> </span><br><span class="line">	concat(round(<span class="built_in">sum</span>(if(datediff(t3.buy_date_second, t3.buy_date_first) <span class="operator">=</span> <span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>)) <span class="operator">/</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="operator">*</span> <span class="number">100</span>, <span class="number">1</span>), <span class="string">&#x27;%&#x27;</span>) percentage</span><br><span class="line"><span class="keyword">from</span> (</span><br><span class="line">	<span class="keyword">select</span> </span><br><span class="line">		t2.user_id,</span><br><span class="line">		<span class="built_in">min</span>(t2.create_date) buy_date_first,</span><br><span class="line">		<span class="built_in">max</span>(t2.create_date) buy_date_second</span><br><span class="line">	<span class="keyword">from</span> (</span><br><span class="line">		<span class="keyword">select</span> </span><br><span class="line">			t1.user_id,</span><br><span class="line">			t1.create_date,</span><br><span class="line">			<span class="built_in">rank</span>() <span class="keyword">over</span> (<span class="keyword">partition</span> <span class="keyword">by</span> t1.user_id <span class="keyword">order</span> <span class="keyword">by</span> t1.create_date) rk</span><br><span class="line">		<span class="keyword">from</span> (</span><br><span class="line">			<span class="keyword">select</span> oi.user_id, oi.create_date </span><br><span class="line">			<span class="keyword">from</span> order_info oi </span><br><span class="line">			<span class="keyword">group</span> <span class="keyword">by</span> oi.user_id, oi.create_date </span><br><span class="line">		) t1</span><br><span class="line">	) t2</span><br><span class="line">	<span class="keyword">where</span> rk <span class="operator">&lt;=</span> <span class="number">2</span></span><br><span class="line">	<span class="keyword">group</span> <span class="keyword">by</span> user_id</span><br><span class="line">) t3;</span><br><span class="line"></span><br><span class="line">percentage<span class="operator">|</span></span><br><span class="line"><span class="comment">----------+</span></span><br><span class="line"><span class="number">60.0</span><span class="operator">%</span>     <span class="operator">|</span></span><br></pre></td></tr></table></figure>



















</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Oct25-X</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://oct25-x.atomgit.net/oct25-xxxxx/2023/07/18/15%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive03_%E6%9F%A5%E8%AF%A2%E5%92%8C%20Hive%20%E5%87%BD%E6%95%B0/">https://oct25-x.atomgit.net/oct25-xxxxx/2023/07/18/15%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive03_%E6%9F%A5%E8%AF%A2%E5%92%8C%20Hive%20%E5%87%BD%E6%95%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://oct25-x.atomgit.net/oct25-xxxxx" target="_blank">Oct25-X</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/oct25-xxxxx/tags/Hive/">Hive</a></div><div class="post_share"><div class="social-share" data-image="/oct25-xxxxx/img/cover_img/ctt15.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/oct25-xxxxx/2023/07/22/16%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive04_%E5%88%86%E5%8C%BA%E3%80%81%E5%88%86%E6%A1%B6%E8%A1%A8%E4%B8%8E%E5%8E%8B%E7%BC%A9/"><img class="prev-cover" src="/oct25-xxxxx/img/cover_img/ctt16.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">大数据之数据仓库Hive04_分区/分桶表与压缩</div></div></a></div><div class="next-post pull-right"><a href="/oct25-xxxxx/2023/07/16/14%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive02_DDL%20%E5%92%8C%20DML/"><img class="next-cover" src="/oct25-xxxxx/img/cover_img/ctt14.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">大数据之数据仓库Hive02_DDL 与 DML</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/oct25-xxxxx/2023/07/16/14%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive02_DDL%20%E5%92%8C%20DML/" title="大数据之数据仓库Hive02_DDL 与 DML"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt14.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-16</div><div class="title">大数据之数据仓库Hive02_DDL 与 DML</div></div></a></div><div><a href="/oct25-xxxxx/2023/07/15/13%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive01_%E5%9F%BA%E7%A1%80/" title="大数据之数据仓库Hive01_基础"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt13.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-15</div><div class="title">大数据之数据仓库Hive01_基础</div></div></a></div><div><a href="/oct25-xxxxx/2023/07/22/16%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive04_%E5%88%86%E5%8C%BA%E3%80%81%E5%88%86%E6%A1%B6%E8%A1%A8%E4%B8%8E%E5%8E%8B%E7%BC%A9/" title="大数据之数据仓库Hive04_分区/分桶表与压缩"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt16.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-22</div><div class="title">大数据之数据仓库Hive04_分区/分桶表与压缩</div></div></a></div><div><a href="/oct25-xxxxx/2023/07/23/17%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93Hive05_%E4%BC%81%E4%B8%9A%E7%BA%A7%E8%B0%83%E4%BC%98/" title="大数据之数据仓库Hive05_企业级调优"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt17.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-07-23</div><div class="title">大数据之数据仓库Hive05_企业级调优</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Oct25-X</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.</span> <span class="toc-text">查询</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%9F%A5%E8%AF%A2%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95"><span class="toc-number">1.1.</span> <span class="toc-text">一、查询基础语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%9F%BA%E7%A1%80%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.2.</span> <span class="toc-text">二、基础查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%87%86%E5%A4%87"><span class="toc-number">1.2.1.</span> <span class="toc-text">数据准备</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E8%A1%A8%E5%92%8C%E7%89%B9%E5%AE%9A%E5%88%97%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.2.2.</span> <span class="toc-text">全表和特定列查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%97%E5%88%AB%E5%90%8D%EF%BC%88AS%EF%BC%89"><span class="toc-number">1.2.3.</span> <span class="toc-text">列别名（AS）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Limit-%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.2.4.</span> <span class="toc-text">Limit 语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Where-%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.2.5.</span> <span class="toc-text">Where 语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E5%87%BD%E6%95%B0"><span class="toc-number">1.2.6.</span> <span class="toc-text">关系运算函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E5%87%BD%E6%95%B0"><span class="toc-number">1.2.7.</span> <span class="toc-text">逻辑运算函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0"><span class="toc-number">1.2.8.</span> <span class="toc-text">聚合函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%88%86%E7%BB%84"><span class="toc-number">1.3.</span> <span class="toc-text">三、分组</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Group-By-%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.3.1.</span> <span class="toc-text">Group By 语句</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Having-%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.3.2.</span> <span class="toc-text">Having 语句</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81Join-%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.4.</span> <span class="toc-text">四、Join 语句</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AD%89%E5%80%BC-Join"><span class="toc-number">1.4.1.</span> <span class="toc-text">等值 Join</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%86%85%E8%BF%9E%E6%8E%A5"><span class="toc-number">1.4.2.</span> <span class="toc-text">内连接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B7%A6-x2F-%E5%8F%B3%E5%A4%96%E8%BF%9E%E6%8E%A5"><span class="toc-number">1.4.3.</span> <span class="toc-text">左&#x2F;右外连接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%BB%A1%E5%A4%96%E8%BF%9E%E6%8E%A5"><span class="toc-number">1.4.4.</span> <span class="toc-text">满外连接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%9A%E8%A1%A8%E8%BF%9E%E6%8E%A5"><span class="toc-number">1.4.5.</span> <span class="toc-text">多表连接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AC%9B%E5%8D%A1%E5%B0%94%E9%9B%86"><span class="toc-number">1.4.6.</span> <span class="toc-text">笛卡尔集</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%81%94%E5%90%88%EF%BC%88union-amp-union-all%EF%BC%89"><span class="toc-number">1.4.7.</span> <span class="toc-text">联合（union &amp; union all）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E6%8E%92%E5%BA%8F"><span class="toc-number">1.5.</span> <span class="toc-text">五、排序</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E6%8E%92%E5%BA%8F%EF%BC%88Order-By%EF%BC%89"><span class="toc-number">1.5.1.</span> <span class="toc-text">全局排序（Order By）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AF%8F%E4%B8%AA-Reduce-%E5%86%85%E9%83%A8%E6%8E%92%E5%BA%8F%EF%BC%88Sort-By%EF%BC%89"><span class="toc-number">1.5.2.</span> <span class="toc-text">每个 Reduce 内部排序（Sort By）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E5%8C%BA%EF%BC%88Distribute-By%EF%BC%89"><span class="toc-number">1.5.3.</span> <span class="toc-text">分区（Distribute By）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E5%8C%BA%E6%8E%92%E5%BA%8F%EF%BC%88Cluster-By%EF%BC%89"><span class="toc-number">1.5.4.</span> <span class="toc-text">分区排序（Cluster By）</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B%E7%BB%83%E4%B9%A0%E4%B8%80"><span class="toc-number">2.</span> <span class="toc-text">综合案例练习一</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%95%B0%E6%8D%AE%E5%87%86%E5%A4%87"><span class="toc-number">2.1.</span> <span class="toc-text">一、数据准备</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BB%BA%E8%A1%A8"><span class="toc-number">2.1.1.</span> <span class="toc-text">建表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E6%96%87%E4%BB%B6"><span class="toc-number">2.1.2.</span> <span class="toc-text">数据文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%BC%E5%85%A5%E6%95%B0%E6%8D%AE"><span class="toc-number">2.1.3.</span> <span class="toc-text">导入数据</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E7%AE%80%E5%8D%95%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.2.</span> <span class="toc-text">二、简单查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E6%B1%87%E6%80%BB%E5%88%86%E6%9E%90"><span class="toc-number">2.3.</span> <span class="toc-text">三、汇总分析</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B1%87%E6%80%BB%E5%88%86%E6%9E%90"><span class="toc-number">2.3.1.</span> <span class="toc-text">汇总分析</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%BB%84"><span class="toc-number">2.3.2.</span> <span class="toc-text">分组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%BB%84%E7%BB%93%E6%9E%9C%E7%9A%84%E6%9D%A1%E4%BB%B6"><span class="toc-number">2.3.3.</span> <span class="toc-text">分组结果的条件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E7%BB%93%E6%9E%9C%E6%8E%92%E5%BA%8F-amp-%E5%88%86%E7%BB%84%E6%8C%87%E5%AE%9A%E6%9D%A1%E4%BB%B6"><span class="toc-number">2.3.4.</span> <span class="toc-text">查询结果排序 &amp; 分组指定条件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%A4%8D%E6%9D%82%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.4.</span> <span class="toc-text">四、复杂查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.4.1.</span> <span class="toc-text">子查询</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.5.</span> <span class="toc-text">五、多表查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%A8%E8%BF%9E%E6%8E%A5"><span class="toc-number">2.5.1.</span> <span class="toc-text">表连接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%9A%E8%A1%A8%E8%BF%9E%E6%8E%A5-1"><span class="toc-number">2.5.2.</span> <span class="toc-text">多表连接</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%87%BD%E6%95%B0"><span class="toc-number">3.</span> <span class="toc-text">函数</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%87%BD%E6%95%B0%E7%AE%80%E4%BB%8B"><span class="toc-number">3.1.</span> <span class="toc-text">一、函数简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%8D%95%E8%A1%8C%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.</span> <span class="toc-text">二、单行函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%97%E6%9C%AF%E8%BF%90%E7%AE%97%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.1.</span> <span class="toc-text">算术运算函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E5%80%BC%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.2.</span> <span class="toc-text">数值函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.3.</span> <span class="toc-text">字符串函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#substring%EF%BC%9A%E6%88%AA%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">3.2.3.1.</span> <span class="toc-text">substring：截取字符串</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#replace%EF%BC%9A%E6%9B%BF%E6%8D%A2"><span class="toc-number">3.2.3.2.</span> <span class="toc-text">replace：替换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#regexp-replace%EF%BC%9A%E6%AD%A3%E5%88%99%E6%9B%BF%E6%8D%A2"><span class="toc-number">3.2.3.3.</span> <span class="toc-text">regexp_replace：正则替换</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#regexp%EF%BC%9A%E6%AD%A3%E5%88%99%E5%8C%B9%E9%85%8D"><span class="toc-number">3.2.3.4.</span> <span class="toc-text">regexp：正则匹配</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#repeat%EF%BC%9A%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">3.2.3.5.</span> <span class="toc-text">repeat：重复字符串</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#split%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%88%87%E5%89%B2"><span class="toc-number">3.2.3.6.</span> <span class="toc-text">split：字符串切割</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#nvl%EF%BC%9A%E6%9B%BF%E6%8D%A2null%E5%80%BC"><span class="toc-number">3.2.3.7.</span> <span class="toc-text">nvl：替换null值</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#concat%EF%BC%9A%E6%8B%BC%E6%8E%A5%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">3.2.3.8.</span> <span class="toc-text">concat：拼接字符串</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#concat-ws%EF%BC%9A%E6%8C%87%E5%AE%9A%E5%88%86%E9%9A%94%E7%AC%A6%E6%8B%BC%E6%8E%A5%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%88%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E7%BB%84"><span class="toc-number">3.2.3.9.</span> <span class="toc-text">concat_ws：指定分隔符拼接字符串或字符串数组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#get-json-object%EF%BC%9A%E8%A7%A3%E6%9E%90json%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">3.2.3.10.</span> <span class="toc-text">get_json_object：解析json字符串</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.4.</span> <span class="toc-text">日期函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#unix-timestamp%EF%BC%9A%E8%BF%94%E5%9B%9E%E5%BD%93%E5%89%8D%E6%88%96%E6%8C%87%E5%AE%9A%E6%97%B6%E9%97%B4%E6%88%B3"><span class="toc-number">3.2.4.1.</span> <span class="toc-text">unix_timestamp：返回当前或指定时间戳</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#from-unixtime%EF%BC%9A%E8%BD%AC%E5%8C%96unix%E6%97%B6%E9%97%B4%E6%88%B3%E5%88%B0%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4"><span class="toc-number">3.2.4.2.</span> <span class="toc-text">from_unixtime：转化unix时间戳到当前时间</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#current-date%EF%BC%9A%E5%BD%93%E5%89%8D%E6%97%A5%E6%9C%9F"><span class="toc-number">3.2.4.3.</span> <span class="toc-text">current_date：当前日期</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#current-timestamp%EF%BC%9A%E5%BD%93%E5%89%8D%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4"><span class="toc-number">3.2.4.4.</span> <span class="toc-text">current_timestamp：当前日期时间</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#month%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E6%97%A5%E6%9C%9F%E4%B8%AD%E7%9A%84%E6%9C%88"><span class="toc-number">3.2.4.5.</span> <span class="toc-text">month：获取指定日期中的月</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#day%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E6%97%A5%E6%9C%9F%E4%B8%AD%E7%9A%84%E6%97%A5"><span class="toc-number">3.2.4.6.</span> <span class="toc-text">day：获取指定日期中的日</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#hour%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E6%97%A5%E6%9C%9F%E4%B8%AD%E7%9A%84%E5%B0%8F%E6%97%B6"><span class="toc-number">3.2.4.7.</span> <span class="toc-text">hour：获取指定日期中的小时</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#datediff%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%97%A5%E6%9C%9F%E7%9B%B8%E5%B7%AE%E5%A4%A9%E6%95%B0"><span class="toc-number">3.2.4.8.</span> <span class="toc-text">datediff：获取日期相差天数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#date-add%EF%BC%9A%E6%97%A5%E6%9C%9F%E5%8A%A0%E5%A4%A9%E6%95%B0"><span class="toc-number">3.2.4.9.</span> <span class="toc-text">date_add：日期加天数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#date-sub%EF%BC%9A%E6%97%A5%E6%9C%9F%E5%87%8F%E5%A4%A9%E6%95%B0"><span class="toc-number">3.2.4.10.</span> <span class="toc-text">date_sub：日期减天数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#date-format%EF%BC%9A%E5%B0%86%E6%97%A5%E6%9C%9F%E8%A7%A3%E6%9E%90%E6%88%90%E6%8C%87%E5%AE%9A%E6%A0%BC%E5%BC%8F%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">3.2.4.11.</span> <span class="toc-text">date_format：将日期解析成指定格式字符串</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.5.</span> <span class="toc-text">流程控制函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#case-when%EF%BC%9A%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.5.1.</span> <span class="toc-text">case when：条件判断函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#if%EF%BC%9A%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD"><span class="toc-number">3.2.5.2.</span> <span class="toc-text">if：条件判断</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9B%86%E5%90%88%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.6.</span> <span class="toc-text">集合函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#array%EF%BC%9A%E5%A3%B0%E6%98%8E%E9%9B%86%E5%90%88"><span class="toc-number">3.2.6.1.</span> <span class="toc-text">array：声明集合</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#size%EF%BC%9A%E9%9B%86%E5%90%88%E5%85%83%E7%B4%A0%E4%B8%AA%E6%95%B0"><span class="toc-number">3.2.6.2.</span> <span class="toc-text">size：集合元素个数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#array-contains%EF%BC%9A%E5%88%A4%E6%96%ADarray%E4%B8%AD%E6%98%AF%E5%90%A6%E5%8C%85%E5%90%AB%E5%85%83%E7%B4%A0"><span class="toc-number">3.2.6.3.</span> <span class="toc-text">array_contains：判断array中是否包含元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#sort-array%EF%BC%9A%E5%B0%86aray%E6%8E%92%E5%BA%8F"><span class="toc-number">3.2.6.4.</span> <span class="toc-text">sort_array：将aray排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#map%EF%BC%9A%E5%88%9B%E5%BB%BAmap%E9%9B%86%E5%90%88"><span class="toc-number">3.2.6.5.</span> <span class="toc-text">map：创建map集合</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#map-keys%EF%BC%9A%E8%BF%94%E5%9B%9Emap%E4%B8%AD%E7%9A%84key"><span class="toc-number">3.2.6.6.</span> <span class="toc-text">map_keys：返回map中的key</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#map-values%EF%BC%9A%E8%BF%94%E5%9B%9Emap%E4%B8%AD%E7%9A%84value"><span class="toc-number">3.2.6.7.</span> <span class="toc-text">map_values：返回map中的value</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#struct%EF%BC%9A%E5%A3%B0%E6%98%8E%E7%BB%93%E6%9E%84%E4%BD%93"><span class="toc-number">3.2.6.8.</span> <span class="toc-text">struct：声明结构体</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#named-struct%EF%BC%9A%E5%A3%B0%E6%98%8E%E7%BB%93%E6%9E%84%E4%BD%93"><span class="toc-number">3.2.6.9.</span> <span class="toc-text">named_struct：声明结构体</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%83%E4%B9%A0"><span class="toc-number">3.2.7.</span> <span class="toc-text">练习</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E9%AB%98%E7%BA%A7%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0"><span class="toc-number">3.3.</span> <span class="toc-text">三、高级聚合函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E7%82%B8%E8%A3%82%E5%87%BD%E6%95%B0"><span class="toc-number">3.4.</span> <span class="toc-text">四、炸裂函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E7%AA%97%E5%8F%A3%E5%87%BD%E6%95%B0%EF%BC%88%E5%BC%80%E7%AA%97%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-number">3.5.</span> <span class="toc-text">五、窗口函数（开窗函数）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E7%AA%97%E5%8F%A3%E5%87%BD%E6%95%B0"><span class="toc-number">3.5.1.</span> <span class="toc-text">常用窗口函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B"><span class="toc-number">3.5.2.</span> <span class="toc-text">示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0"><span class="toc-number">3.6.</span> <span class="toc-text">六、自定义函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89-UDF-%E5%87%BD%E6%95%B0"><span class="toc-number">3.7.</span> <span class="toc-text">七、自定义 UDF 函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9C%80%E6%B1%82"><span class="toc-number">3.7.1.</span> <span class="toc-text">需求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89-UDF-%E5%87%BD%E6%95%B0%E5%AE%9E%E7%8E%B0"><span class="toc-number">3.7.2.</span> <span class="toc-text">自定义 UDF 函数实现</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B%E7%BB%83%E4%B9%A0%E4%BA%8C"><span class="toc-number">4.</span> <span class="toc-text">综合案例练习二</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87"><span class="toc-number">4.0.1.</span> <span class="toc-text">环境准备</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF%E8%A1%A8"><span class="toc-number">4.0.1.1.</span> <span class="toc-text">用户信息表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%95%86%E5%93%81%E4%BF%A1%E6%81%AF%E8%A1%A8"><span class="toc-number">4.0.1.2.</span> <span class="toc-text">商品信息表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%95%86%E5%93%81%E5%88%86%E7%B1%BB%E4%BF%A1%E6%81%AF%E8%A1%A8"><span class="toc-number">4.0.1.3.</span> <span class="toc-text">商品分类信息表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%A2%E5%8D%95%E4%BF%A1%E6%81%AF%E8%A1%A8"><span class="toc-number">4.0.1.4.</span> <span class="toc-text">订单信息表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%A2%E5%8D%95%E6%98%8E%E7%BB%86%E8%A1%A8"><span class="toc-number">4.0.1.5.</span> <span class="toc-text">订单明细表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%99%BB%E5%BD%95%E6%98%8E%E7%BB%86%E8%A1%A8"><span class="toc-number">4.0.1.6.</span> <span class="toc-text">登录明细表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%95%86%E5%93%81%E4%BB%B7%E6%A0%BC%E5%8F%98%E6%9B%B4%E6%98%8E%E7%BB%86%E8%A1%A8"><span class="toc-number">4.0.1.7.</span> <span class="toc-text">商品价格变更明细表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E9%80%81%E4%BF%A1%E6%81%AF%E8%A1%A8"><span class="toc-number">4.0.1.8.</span> <span class="toc-text">配送信息表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A5%BD%E5%8F%8B%E5%85%B3%E7%B3%BB%E8%A1%A8"><span class="toc-number">4.0.1.9.</span> <span class="toc-text">好友关系表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%94%B6%E8%97%8F%E4%BF%A1%E6%81%AF%E8%A1%A8"><span class="toc-number">4.0.1.10.</span> <span class="toc-text">收藏信息表</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A1%88%E4%BE%8B%E7%BB%83%E4%B9%A0"><span class="toc-number">4.0.2.</span> <span class="toc-text">案例练习</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E7%B4%AF%E7%A7%AF%E9%94%80%E9%87%8F%E6%8E%92%E5%90%8D%E7%AC%AC%E4%BA%8C%E7%9A%84%E5%95%86%E5%93%81"><span class="toc-number">4.0.2.1.</span> <span class="toc-text">查询累积销量排名第二的商品</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E8%87%B3%E5%B0%91%E8%BF%9E%E7%BB%AD%E4%B8%89%E5%A4%A9%E4%B8%8B%E5%8D%95%E7%9A%84%E7%94%A8%E6%88%B7"><span class="toc-number">4.0.2.2.</span> <span class="toc-text">查询至少连续三天下单的用户</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E5%90%84%E5%93%81%E7%B1%BB%E5%95%86%E5%93%81%E4%B8%AD%EF%BC%8C%E9%94%80%E9%87%8F%E6%9C%80%E9%AB%98%E7%9A%84%E5%95%86%E5%93%81id%EF%BC%8C%E5%95%86%E5%93%81%E5%90%8D%EF%BC%8C%E5%95%86%E5%93%81%E7%9A%84%E9%94%80%E9%87%8F%EF%BC%8C%E4%BB%A5%E5%8F%8A%E5%BD%93%E5%89%8D%E5%93%81%E7%B1%BB%E5%95%86%E5%93%81%E7%9A%84%E6%95%B0%E9%87%8F"><span class="toc-number">4.0.2.3.</span> <span class="toc-text">查询各品类商品中，销量最高的商品id，商品名，商品的销量，以及当前品类商品的数量</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E7%94%A8%E6%88%B7%E7%9A%84%E7%B4%AF%E8%AE%A1%E6%B6%88%E8%B4%B9%E9%87%91%E9%A2%9D%E5%8F%8AVIP%E7%AD%89%E7%BA%A7"><span class="toc-number">4.0.2.4.</span> <span class="toc-text">查询用户的累计消费金额及VIP等级</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E9%A6%96%E6%AC%A1%E4%B8%8B%E5%8D%95%E5%90%8E%E7%AC%AC%E4%BA%8C%E5%A4%A9%E8%BF%9E%E7%BB%AD%E4%B8%8B%E5%8D%95%E7%9A%84%E7%94%A8%E6%88%B7%E6%AF%94%E7%8E%87"><span class="toc-number">4.0.2.5.</span> <span class="toc-text">查询首次下单后第二天连续下单的用户比率</span></a></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"><img src="/oct25-xxxxx/img/cover_img/ctt52.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型">大数据之多范式编程语言 Scala09_隐式转换、异常和泛型</a><time datetime="2024-02-24T04:55:34.000Z" title="发表于 2024-02-24 12:55:34">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配"><img src="/oct25-xxxxx/img/cover_img/ctt51.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala08_模式匹配"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配">大数据之多范式编程语言 Scala08_模式匹配</a><time datetime="2024-02-24T00:46:12.000Z" title="发表于 2024-02-24 08:46:12">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img src="/oct25-xxxxx/img/cover_img/ctt50.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala07_集合"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合">大数据之多范式编程语言 Scala07_集合</a><time datetime="2024-02-22T08:16:10.000Z" title="发表于 2024-02-22 16:16:10">2024-02-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala06_面向对象"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象">大数据之多范式编程语言 Scala06_面向对象</a><time datetime="2024-02-21T07:01:10.000Z" title="发表于 2024-02-21 15:01:10">2024-02-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img src="/oct25-xxxxx/img/cover_img/ctt48.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala05_函数式编程"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程">大数据之多范式编程语言 Scala05_函数式编程</a><time datetime="2024-02-20T12:05:11.000Z" title="发表于 2024-02-20 20:05:11">2024-02-20</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg14.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Oct25-X</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></a><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/oct25-xxxxx/js/utils.js"></script><script src="/oct25-xxxxx/js/main.js"></script><script src="/oct25-xxxxx/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/oct25-xxxxx/js/search/local-search.js"></script><div class="js-pjax"></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>